• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on closing ResultSets

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the Javadocs for ResultSet,

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.



Why, then, do I keep seeing examples like this?

Isn't closing the ResultSet superfluous, since the Statement is being closed right afterwards?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Cano wrote:Isn't closing the ResultSet superfluous, since the Statement is being closed right afterwards?


Yes. However some drivers don't implement the spec properly so people got in the habit of closing both. I do the same. No harm in it.
 
Chris Cano
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, thanks.

How about the part about when the Statement is re-executed? If I have a PreparedStatement executing a query in a loop (or just any reuse of a PreparedStatement), do I need to close the ResultSet from each execution of the PreparedStatement?
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should close the result set if you are going to reuse a statement. While not closing it won't kill your application, if the life of the statement is significantly long, you could end up with a lot of unclosed result sets. Keep in mind, garbage collection will not close an object. As Jeanne pointed out, though, you should always close both when you're done with them since you can't predict what a driver is going to do and because its a good practice. Not closing JDBC objects properly can lead to a lot of bugs as an application scales to more and more users.

I wrote an article about closing JDBC objects a few years back you might find of use.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic