Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Proper way to closing jdbc connection and result sets?

 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 7 to the rescue:
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic