• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of closure of DB resources in JDBC

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic