views:

194

answers:

2

Is it safe to do connection.createStatement().executeQuery(String query);, or should each java.sql.Statement object be created and closed by "hand" on every use, even if the connection is closed in a finally block?

If multiple queries are executed in one method by one connection it would use much less code if instantiating and closing each statement wasn't strictly necessary.

+7  A: 

It's true that closing a Connection in almost all JDBC driver implementations also implicitly closes the Statement. But, a big but, when you're using a connection pool (and you would like to use one because that greatly improves connecting performance), the close() will not close the Connection, but release it back to the pool, hereby leaving the Statement open. And when you do this too often, you will run out of resources sooner or later which may kill your application.

Always acquire and close all the three resources Connection, Statement and ResultSet in the shortest possible scope, no excuses.

BalusC
Yeah, nothing is going to teach you to use try/finally faster than running out of resources in production one fine day.
Alexander Pogrebnyak
As someone who's worked Saturdays because lazy developers didn't understand you can't re-use the same Statement reference with pooled connections (thus running the database out of cursors), I would like to double-encourage you to heed BalusC's words!
Affe
Ah ha! Though I am not using a connection pool at the moment (no real current need in my mind) it seems like it would make my life easier in the future if I closed and used everything "correctly" now and decide to use a pool in the future. Thanks!
javanix
Exactly! You're welcome :)
BalusC
A side note, because I see in your question history that you're doing webapps, I just want to warn you about this bit: when you use a container managed datasource to have an easy management of DB connectivity, you should realize that it's underhand usually already a connection pool!
BalusC
+1  A: 

Move all the boilerplate code to a helper method/class.

That way you can acquire/release resource in the manner consistent with JDBC (or any other limited resource package) documentation and yet have your business logic not be polluted by JDBC clutter.

And though it may seem like a waste to you now, but there is nothing wrong with dedicating single query to a single method in your business logic class.

Alexander Pogrebnyak
Could you elaborate on that last point a bit? "there is nothing wrong with dedicating single query to a single method in your business logic class."I think you are talking about some sort of query wrapper, but I'm curious how you would implement something of the sort that doesn't have gigantic set of arguments.
javanix
@javanix. It looked from your original question that you want to run a few SQL commands in a single method and are concerned with necessary SQL clutter. The last paragraph suggests that you can dedicate a single method to run a single query that's sets up and tears down JDBC artifacts. As I don't know anything about your requirements from your original posting this may not be practical in your case. Also remember, gigantic set or arguments can be hidden behind a concrete implementation of a simple interface. That is what are classes for, to carry the state information.
Alexander Pogrebnyak