我用静态代码分析器扫描了我的代码,得到了未发布的资源:数据库错误。我正在关闭以下所有db连接,这是我的代码的快照。
public String methodInDAO(Bean bean) throws SQLException ,Exception
{
Session session = null;
Connection connection = null;
ResultSet resultSet1 = null;
CallableStatement callableStatement = null;
try {
connection = dataSource.getConnection();
callableStatement = connection.prepareCall(query);
resultSet1 = callableStatement.execute();
//code operations
} finally {
if(null != callableStatement)
callableStatement.close();
resultSet1 = null;
callableStatement = null;
if(null != connection)
connection.close();
if (null != session)
session.close();
}
return returnOutput;
}所有抛出的异常都在服务层处理。有人能建议哪里没有发布数据源吗?
发布于 2014-10-20 12:46:39
如果您的JDBC驱动程序支持JDBC 4.1,则可以使用试着用资源。
try (connection = dataSource.getConnection();
callableStatement = connection.prepareCall(query)) {
results = callableStatement.execute();
// code operations
}使用try-with-resources语句自动关闭
Connection、ResultSet和Statement类型资源的能力。
添加到JDBC 4.1中。
https://stackoverflow.com/questions/26466009
复制相似问题