背景
我正在使用和关闭很多 Prepared/Callable Statements 和 ResultSet,所以我在 finally 部分创建了一个全局静态方法来清理。
很明显,大多数时候这些不能为空,我只想确保添加空检查而不是立即关闭。
到目前为止,使用这种方法完全没有问题,但是这个线程安全吗?如果没有,我是否需要使用同步方法而不是普通的静态方法?
用法其他线程类
private PreparedStatement psUpdate = null;
try {....}
catch(Exception e) {...}
finally
{
Utils.NullCheckClose(this.psUpdate, this.getProcName());
}
声明实用程序类
public static void NullCheckClose(PreparedStatement temp, String threadname)
{
try
{
if(temp != null)
temp.close();
}
catch(Exception msg)
{
Logger.erLog(msg, threadname);
}
finally
{
temp = null;
}
}
public static void NullCheckClose(CallableStatement temp, String threadname)
{
try
{
if(temp != null)
temp.close();
}
catch(Exception msg)
{
Logger.erLog(msg, threadname);
}
finally
{
temp = null;
}
}
public static void NullCheckClose(ResultSet temp, String threadname)
{
try
{
if(temp != null)
temp.close();
}
catch(Exception msg)
{
Logger.erLog(msg, threadname);
}
finally
{
temp = null;
}
}