• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Order of closure of DB resources in JDBC

 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Does Order of closure of DB resources in JDBC transaction have any adverse (unwanted or erroneous) effects ?

For example - if I close statement first and then result set later .... will cause any error ?



Thanks
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose it might. You could spend some time going through the API documentation, or you could spend some time writing a small piece of code and running it. If it were me I would go for the small piece of code.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for your information: if you use try-with-resources (since Java 7), the resources will be (automatically) closed in reverse order of the order in which they are created. So using this example, the ResultSet will be closed first, followed by the Statement and finally the Connection

If you have a look at the API documentation of the close() method of the Statement interface, you'll notice this (important) note:

Java Platform, Standard Edition 8 API Specification - Statement wrote:Note: When a Statement object is closed, its current ResultSet object, if one exists, is also closed.



Hope it helps!
Kind regards,
Roel
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or if you are still stuck on Java 6, you can do it like this. I know this is wordy, but I'm a belt and suspenders kind of guy.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:Or if you are still stuck on Java 6, you can do it like this. I know this is wordy, but I'm a belt and suspenders kind of guy.


And if you want to get rid of the "wordiness", you simply create a utility class with 3 static methods to close a result set, statement and connection. For exampleAnd then you can have a nice and clean finally blockAnd adding an additional method in DbUtils taking 3 parameters will reduce the finally block to just one line of code.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That's the basic structure (working from memory) to do this without having to check for nulls.
Note, you might want to wrap those closes in try/catches so you don't lose the original problem.
Of course, this is pre try-with-resources.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:That's the basic structure (working from memory) to do this without having to check for nulls.


Wondering if that's a better approach than using the null checks as it looks like the Arrow Anti Pattern.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other ways have their own problems.

Declaring all the bits (conn, ps and rs) outside a single try/catch means you are allowing stuff to get outside the scope they should live in.

It's a side effect of all the boiler plate.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:
And if you want to get rid of the "wordiness", you simply create a utility class with 3 static methods to close a result set, statement and connection. For example


That is beautiful in it's simplicity. I'm going to create that today.

That's cow-worthy!
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic