结果集资源泄漏,即使我正在关闭RS



我得到'资源泄漏:'rsHP'未在此位置关闭',我使用rsHP = stmt.executeQuery(query);

下面是这个方法的基本布局…

public static void method(String x, Connection conn){
Statement stmtHP = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
ResultSet rsHP = null;
try{
      ----ALGORITHM IN HERE------
      ****This is the general form of this method*****
      queryHP = "select * from SOMETABLE where SOMETHING = 'blah'";
      rsHP = stmtHP.executeQuery(queryHP);
      while(rsHP.next()){
            List.add(rsHP.getString("COLNAME"));
      }  
            .
            .
        repeats for 8 different queries
            .
            .   
      queryHP = "select * from SOMEOTHERTABLE where SOMETHINGELSE = 'blah2'";
      rsHP = stmtHP.executeQuery(queryHP);
      while(rsHP.next()){
            List.add(rsHP.getString("NEWCOLNAME"));
      } 
}catch(Exception e){
       System.out.println("Hey dumbo you suck, Exception Found");
       rsHP.close();
       stmtHP.close();
       conn.close();
}finally{
       rsHP.close();
       stmtHP.close();
       // connection gets closed later if no exceptions thrown
}
}// end method

在这里结束时,我显然关闭了我所有的东西。我很困惑,如果我的方法不可能在抛出错误之外不关闭RS的情况下终止,我是如何有内存泄漏的。

Connection#createStatement()抛出一个SQLException,因此此代码根本无法编译。

我建议你把方法的签名改为
public static void method(String x, Connection conn) throws SQLException

对于资源泄漏,我想使用以下逻辑将帮助您

try{
    // code
    rsHP.close();
    conn.close();
}catch(Exception e){
    // StackTrace
}finally{
    if (rsHP != null) rsHP.close();
    if (conn != null) conn.close();
}

相关内容

  • 没有找到相关文章

最新更新