程序不应依赖于虚拟机的finalize()方法自动的回收数据库资源,而应手动在finally代码块中进行数据库资源释放操作。
Connection conn=null;
PreparedStatement stmt=null;
try{
conn = DriverManager.getConnection(some_connection_string);
stmt = conn.prepareStatement(sqlquery);
...
} catch(SQLException e){
log(e);
}finally{
if(con!=null){
try {
conn.close();
} catch (SQLException e) {
log(e);
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
log(e);
}
}
}