• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does it make a difference?  RSS feed

 
George Larry
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this is a JSP question or a database question, but I have a JSP page that makes several calls to the db. All calls are independent of each other so I use the same ResultSet.
My question is- do I need to rs.close() my ResultSet between uses? Does it make a difference- in memory usage, efficiency, speed, etc?
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd consider this a JDBC question, but I don't understand it, since ResultSets are returned by database queries, so a different one should be created by each query. In such a case, you'd definitely want to close them when you're done so as to release resources as quickly as possible.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. Remember that there's a difference between a VARIABLE and the thing that is held by the variable. If you're doing something like this:
ResultSet rs = stmt1.executeQuery();
// do stuff
rs = stmt2.executeQuery();
// do more stuff
Despite the fact that you're reusing the "rs" variable, you've actually got two different objects. Each call to executeQuery() returns a new ResultSet.
So, follow the previous advice and always remember to close your result sets when you're done. Personally, I find it easiest to NEVER reuse variables like this. That makes it clearer what your intention is. So my code would look more like:
ResultSet rs1 = stmt1.executeQuery();
// do stuff
rs1.close();
ResultSet rs2 = stmt2.executeQuery();
// do more stuff
rs2.close();
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
[This message has been edited by Kyle Brown (edited November 15, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!