File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Proper way to closing jdbc connection and result sets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Proper way to closing jdbc connection and result sets?" Watch "Proper way to closing jdbc connection and result sets?" New topic
Author

Proper way to closing jdbc connection and result sets?

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2346
    
    7

Hello ranchers

I want to ask does closing the database connection in the finally block also closes the result set and/or prepared statement or callable statement? Or should I close each resource individually? Another question is if such resource cannot be closed properly and throws exception, these are ignored by JVM, am I correct?



Thanks in advanced.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

While closing a connection should close all resultsets and statements opened on that connection, I'd say it is better to close these too. If you later refactor the method and stop closing the connection there, you might forget to add the code to close the statements and resultsets. There is a caveat, though:
K. Tsang wrote:Another question is if such resource cannot be closed properly and throws exception, these are ignored by JVM, am I correct?

This is not true. If an exception occurs in a finally block, it is thrown as any other exception. The original exception will be lost in this case.

A solution I like is to declare a helper method somewhere:

and use this method (and similar one for resultsets) to close the resources.

Or use Java 7 and it's try-with-resources syntax, which takes care of all this!
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2346
    
    7

Thanks Martin for your prompt response.

So for the exception in the finally block if there are say 3 close() statements. Will the 2nd and 3rd line execute if the 1st statement throws an exception? In another word, will the code follow through closing all the resources?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Java 7 to the rescue:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

K. Tsang wrote:So for the exception in the finally block if there are say 3 close() statements. Will the 2nd and 3rd line execute if the 1st statement throws an exception? In another word, will the code follow through closing all the resources?

Sorry, I've missed this question.

The answer is 'No'. If there is an exception thrown in the finally block, and it is not caught inside the same finally block, the finally block is terminated by throwing that exception. Moreover, if the finally block was entered due to an uncaught exception, the original exception is lost and the stack unwinds using the new exception.

Java 7 handles all this. There is a new concept of suppressed exception (see the docs) which allows to somehow handle several "sibling" occurrences of exceptions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Proper way to closing jdbc connection and result sets?