0

I am getting 'Resource leak: 'rsHP' is not closed at this location' everywhere I use a rsHP = stmt.executeQuery(query);

Here is a basic layout of what this method does...

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

At the end here I am clearly closing all my stuff. I am confused as to how I have a memory leak if it is literally impossible for my method to terminate without closing the RS outside of an error being thrown.

Cody
  • 65
  • 3

1 Answers1

1

Connection#createStatement() throws an SQLException so this code would not compile at all.

I suggest you change the signature of the method to

public static void method(String x, Connection conn) throws SQLException

For the resource leak, I guess using the following logic will help you

try{
    // code
    rsHP.close();
    conn.close();
}catch(Exception e){
    // StackTrace
}finally{
    if (rsHP != null) rsHP.close();
    if (conn != null) conn.close();
}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • 1
    I had the throws sql exception, i was just trying to be brief, i have some internal if else error catching that i think is conflicting with the try catch, so i will probably clean that up and see if it fixes the issue. I think you're right on adding the close's to the end of the try, removing them from finally, and adding the if's to the finally. Thanks for the input! – Cody Jul 01 '16 at 23:50