my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Error catching for DataSource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error catching for DataSource" Watch "Error catching for DataSource" New topic
Author

Error catching for DataSource

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

This newbie tutorial , includes an example of using DataSource, where the author closed the connections to the database within the try block, rather than, what I would do, within the finally block.

Do you agree with this approach? Why?




Jon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
I would agree with you about using the finally block. There is another problem in that code however; each of the three things to be closed might throw an Exception, so your finally block might complte abruptly. So you should maybe have three try-finally pairs, one to open the connection, one the statement and one the result set. And of course to close them again.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

In other words:
Or, with Java 7:
If you need to call some methods on conn or stmt before using them you may want to split them into multiple try-with-resources blocks, but the code will be almost equally clean.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
Thank you, Rob.

Jon Camilleri: are you sure you have copied everything correctly from the tutorial? When I looked, there was a finally with a close() call in.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Campbell Ritchie wrote:Thank you, Rob.

are you sure you have copied everything correctly from the tutorial? When I looked, there was a finally with a close() call in.


In the tutorial the PrintWriter object is closed within the finally block, whilst my question was about the Connection object.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I personally handle this situation by enclosing try finally with try catch

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
Rob's solution is neater, I think.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
Seetharaman Venkatasamy wrote:. . . by enclosing try finally with try catch . . .
That is where you will need the != null tests in the finally blocks.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Campbell Ritchie wrote:That is where you will need the != null tests in the finally blocks.


in closeDBResources(Connection conn, Statement stmt, ResultSet result):


In Rob code, I just feel the many nested try finally may make less readability! but what I made the method above encapsulate the closing part. is not it? correct me If I am wrong please
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Seetharaman Venkatasamy wrote:
Campbell Ritchie wrote:That is where you will need the != null tests in the finally blocks.


in closeDBResources(Connection conn, Statement stmt, ResultSet result):


In Rob code, I just feel the many nested try finally may make less readability! but what I made the method above encapsulate the closing part. is not it? correct me If I am wrong please


Yes, the nested statements are a bit tough to digest, in fact I preferred the Java 7 part suggested by Rob Spoor, which I am planning to practice shortly.

Why are you enclosing each .close() within a try..catch block, rather than grouping them? I'm afraid this would be slightly less efficient than grouping them, hence, JVM would process the nested statements faster, than a series of try..catch blocks. What do you think?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Seetharaman Venkatasamy wrote:In Rob code, I just feel the many nested try finally may make less readability! but what I made the method above encapsulate the closing part. is not it? correct me If I am wrong please

I don't like to either include or re-implement a utility class all over the place, so my approach will work everywhere. However, once I switch to Java 7, I'll definitely use the try-with-resources version. It's shorter, and handles the closing for you.

Jon Camilleri wrote:Why are you enclosing each .close() within a try..catch block, rather than grouping them? I'm afraid this would be slightly less efficient than grouping them, hence, JVM would process the nested statements faster, than a series of try..catch blocks. What do you think?

Because each of the close() methods can throw an SQLException. If that occurs, you still want the others to be closed (if possible). But again, this is something that the try-with-resources will do for you.
 
wood burning stoves
 
subject: Error catching for DataSource
 
Similar Threads
PreparedStatement and NullPointerException
Purpose of Finally Block
Exception
how ro handle exceptions in finally block .please help me thanks inadvance
close sql connection in finally block