File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Reagarding ResultSet Close in a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Reagarding ResultSet Close in a Thread" Watch "Reagarding ResultSet Close in a Thread" New topic

Reagarding ResultSet Close in a Thread

Venkatesh Rajendran
Ranch Hand

Joined: May 17, 2001
Posts: 59
Hi All,
I have one basic confirmation, please excuse if you feel it is very basic. Is it ok to close Statement alone? will it intern close ResultSet? or it is mandatory to close ResultSet explicitely? please confirm.
I am executing a set of statements inside a Thread continuously where I am not closing ResultSet. I just want to know will it cause a problem.
Thanks in advance.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
I think this snippet from the Statement.close() javadocs will anser your question:
It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources.
When a Statement object is closed, its current ResultSet object, if one exists, is also closed.

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I agree with Blake in that "in theory" all is fine and dandy. Just beware that he quoted the javadocs. This does not in turn guarantee that an implementation follows this rule. Be safe, and close all your resultsets when you are finished with them.
Marshall B Thompson
Ranch Hand

Joined: Apr 11, 2002
Posts: 42
Explicitly close objects! I went looking to see if this was mentioned in my database (oracle) doc. From the oracle 9i jdbc manual:
"If you receive messages that you are running out of cursors or that you are running out of memory, make sure that all your Statement and ResultSet objects are explicitly closed. The Oracle JDBC drivers do not have finalizer methods. They perform cleanup routines by using the close() method of the ResultSet and Statement classes. If you do not explicitly close your result set and statement objects, significant memory leaks can occur. You could also run out of cursors in the database. Closing a result set or statement releases the corresponding cursor in the database.
Similarly, you must explicitly close Connection objects to avoid leaking and running out of cursors on the server side. When you close the connection, the JDBC driver closes any open statement objects associated with it, thus releasing the cursor objects on the server side."
I agree. Here's the link:
subject: Reagarding ResultSet Close in a Thread
It's not a secret anymore!