• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Error catching for DataSource

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?



 
Marshal
Posts: 75664
354
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 75664
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I personally handle this situation by enclosing try finally with try catch

 
Campbell Ritchie
Marshal
Posts: 75664
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob's solution is neater, I think.
 
Campbell Ritchie
Marshal
Posts: 75664
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
It's just a flesh wound! Or a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic