• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Closing ResultSet and Statement

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 427
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. You confirmed my suspicions.
 
Don't touch me. And dont' touch this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic