Jaque
Jaque

Reputation: 5

Where to close result set

I am getting result set is already closed where i am passing resultset into other method.where should i closed result set.

public void mainTest(){

ResultSet rs= pstmt.executeQuery(query);

List list = populateRS(rs);

if(rs!=null)rs.close();
}

public List populateRS(ResultSet rs){

//work with result set
if(rs!=null)rs.close();
}

Upvotes: 0

Views: 557

Answers (6)

Mike Samuel
Mike Samuel

Reputation: 120496

Close in the same method you open in if at all possible. Consistently doing this makes it easy for code-reviewers and maintainers to easily triage resources into (obviously freed, obviously problematic, and needs more attention).

A few other notes:

  1. Use try (...) or do the closing in finally so the resource is closed even when the code using it fails with an exception.
  2. Use the @WillClose and @WillNotClose annotations as appropriate so that IDEs and tools like findbugs can point out problems.

public void mainTest(){
  List<?> list;
  try (ResultSet rs = pstmt.executeQuery(query)) {
    list = populateRS(rs);
  }
  // work with list
}

public List<?> populateRS(@WillNotClose ResultSet rs){
  //work with result set
}

or if you're stuck with older Java:

public void mainTest(){
  List<?> list;
  ResultSet rs = pstmt.executeQuery(query);
  try {
    list = populateRS(rs);
  } finally {
    if(rs!=null)rs.close();
  }
  // work with list
}

Upvotes: 5

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Use the new try-with-resources statement which would automatically close the ResultSet whether an exception occurs or not because it implements AutoCloseable.

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.

public void mainTest()
{
  try (ResultSet rs = pstmt.executeQuery(query)) { 
     List list = populateRS(rs);
  } catch (SQLException ex) {

  }
}

public List populateRS(ResultSet rs){
    // work with result set
}

Upvotes: 3

JHS
JHS

Reputation: 7871

You should close the ResultSet in the mainTest method to segregate populateRS method's participation in the lifecycle of the ResultSet.

You should have a finally block in which you should close the ResultSet. This practice gives you a guarantee that the ResultSet is closed even if an exception is thrown.

Upvotes: 0

Prabhaker A
Prabhaker A

Reputation: 8473

It is good to close where you are opening .
It is good programming practise to close all resouces in finally block

       public void mainTest()
       {
         ResultSet rs = null;
         try{ 
              rs= pstmt.executeQuery(query);
             List list = populateRS(rs);
          }finally{
               try {
                 rs.close();
               } catch (SQLException ex) {

               }
          }
      }

     public List populateRS(ResultSet rs){

        //work with result set

    }

according to java docs

Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Upvotes: 2

andersschuller
andersschuller

Reputation: 13907

You should probably use a try-finally block to close the ResultSet even if populateRS (or something else) throws an exception:

ResultSet rs;
try {
    rs = pstmt.executeQuery(query);
    List list = populateRS(rs);
} finally {
    if (rs != null) {
        rs.close();
    }
}

Upvotes: 5

Nathan Hughes
Nathan Hughes

Reputation: 96385

Close things near where you open them. In this case that would be in the mainTest method after you call populateRS. If a method doesn't open something, it shouldn't close it.

Upvotes: 1

Related Questions