This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Closing ResultSet and Statement

 
Mila Shanoy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am cleaning up/debugging the following code:
stmt = conn.createStatement();
stmt.execute(some_select_query1);
rs = stmt.getResultSet();
// ..... some code that uses rs ....
stmt.execute(some_select_query2);
rs = stmt.getResultSet();
// ..... some code that uses rs ....

My question is: does the code leave hanging resultset objects? Does it need rs.close() before each 'rs = stmt.getResultSet();'
What is your opinion/experience on that?
Thank you.
 
Sean Sullivan
Ranch Hand
Posts: 427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend closing each ResultSet by calling close()
I usually close the ResultSet and the Statment in a finally block.
 
James Redpath
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have ran into this myself. Here's the story from Oracle. The drivers do not have finializer methods; cleanup routines are performed by the close() method of the ResultSet and Statement classes. If you do not explicitly close your ResultSet and Statement objects (I stress AND here), serious memory leaks could occur. Plus you could run out of cursors in the database. (this has happen to me). Closing the result set or statment releases the corrsponding cusor in the database. So close for each rs/stmt, your connection will still stay open! Then create rs/stmt again
s = new String("Select * from user_tables");
Try{ stmt=conn.createStatement();
rs = stmt.executeQuery(s);
rs.close();
stmt.close();
}catch (SQLException sqle) {}
.... some building of new string
Try{ stmt=conn.createStatement();
rs = stmt.executeQuery(s);
rs.close();
stmt.close();
}catch (SQLException sqle) {}
 
Mila Shanoy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. You confirmed my suspicions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic