关于 JDBC 资源回收的讨论 !
<strong>public</strong>
<strong>static</strong>
<strong>void</strong> close(java.sql.ResultSet rs,
java.sql.PreparedStatement pst,
java.sql.Connection conn
) {
if (rs != null) { try { rs.close(); rs = null; } catch (SQLException se) { se.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException se) { se.printStackTrace(); } rs = null; } } }
if (pst != null) { try { pst.close(); pst = null; } catch (SQLException ex) { ex.printStackTrace(); } finally { if (pst != null) { try { pst.close(); } catch (SQLException se) { se.printStackTrace(); }
pst = <strong>null</strong>;
}
}
}
if (conn != null) { try { conn.close(); conn = null; } catch (SQLException ex) { ex.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException se) { se.printStackTrace(); } conn = null; } } } } </code>
上面有人说 close 方法必须放在 finally 里,不知道基于什么考虑。
数据库资源与普通 Object 不一样的。应该说建议还是要即时、完全地关闭,也有利于 pooling 来工作。 我想是这样,应该说你的 close() 是由 pooling 的实现来?BR>
排版太乱了吧,看起来很吃力。