範例
protected
void
finalize()
throws
Throwable {
try
{
getContext().close();
}
catch
(
NamingException
exc) {
LogUtility.log(exc);
}
finally
{
super
.finalize();
}
}
private
Context
getContext() {
return
_context;
}
private
transient
Context
_context;
解決方案
使用明確呼叫的清理方法,代替 finalize() 方法。
public
void
free() {
try
{
getContext().close();
}
catch
(
NamingException
exc) {
LogUtility.log(exc);
}
}
private
Context
getContext() {
return
_context;
}
private
transient
Context
_context;