如果我通过在连接对象上调用 close() 显式关闭连接,我已将连接对象设置为 null。连接对象上的 close() 和 null 有什么区别?如果我关闭连接,连接池中仍然维护连接对象吗?例如
Connection dbConnection=null;
PreparedStatement preparedStatement = null;
ResultSet rs;
try {
Connection dbConnection= DriverManager.getConnection("jdbc:hsqldb:file:test5","sa", "");
...........
...........
dbConnection.close();
dbConnection=null;
} catch (Exception e) {
LOGGER.error("Exception Occured while fetching All record:Item details start method "
+ e.getMessage());
} finally {
try
{
if (rs!=null)
{
rs.close();
rs=null;
}
}
catch(SQLException e)
{
LOGGER.error(RESULTSETCLOSEEXCEPTION
+ e.getMessage());
}
try {
if (preparedStatement != null) {
preparedStatement.close();
preparedStatement=null;
}
} catch (SQLException e) {
LOGGER.error(STATEMENTCLOSEEXCEPTION
+ e.getMessage());
}
try {
if (dbConnection != null) {
dbConnection.close();
dbConnection=null;
}
} catch (SQLException e) {
LOGGER.error(CONNECTIONCLOSEEXCEPTION
+ e.getMessage());
}
}
上面的代码是关闭连接、准备好的语句和结果集的正确方法吗?