JDBC中的语句泄漏

时间:2014-12-09 05:24:08

标签: java jdbc

JDBC中的语句泄漏:

  pstmt =
StatementLeakage : An open JDBC Statement is not closed on all paths. This can cause a transaction or Statement resources to remain active indefinitely, slowing or preventing access to the database by other requests.: for (object created at line = TunnelDBHandler:139, type = java.sql.PreparedStatement), object used at prepareStatement() @ TunnelDBHandler:139 
                 dbManager
                    .getConnection()
                   .prepareStatement(
                      "update TUNNEL_STORE set IS_TUNNEL_OPEN=? where TUNNEL_ID=?");

1 个答案:

答案 0 :(得分:2)

添加finally屏蔽,并在您的所有ResultSetStatementConnection(s)上致电close()。作为一个非常粗略的例子,

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
  // ...
  // get a statement for stmt
  // get a resultset from the stmt
  // ...
  while (rs.next()) {

  }
} catch (SQLException e) {
  e.printStackTrace();
} finally {
  if (rs != null) {
    try {
      rs.close();
    } catch (SQLException e) {
    }
  }
  if (stmt != null) {
    try {
      stmt.close();
    } catch (SQLException e) {
    }
  }
  if (conn != null) {
    try {
      conn.close();
    } catch (SQLException e) {
    }
  }
}