So from an architectural standpoint, it would probably be best to only use QueryRunner with DataSources so that connection management is handled by DBUtils?
Thanks, John
Main Topics
Browse All TopicsI am switching all my J2EE database code over to Apache dbUtils and dbcp. I've read through the documentation and in general it all looks nice and neat. The one thing I don't have clear in my head is where the responsibility lies for closing connections. It is suggested in the API that if you pass a java.sql.Connection to a method, then you are responsible for closing that connection. Does this mean that if a Connection is not explicitly passed (i.e. the QueryRunner uses the DataSource to manage its own connection) that I do not need to do anything with the connection? That would be nice, but sounds too good to be true ;-)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: Ajay-SinghPosted on 2009-03-22 at 18:15:02ID: 23954238
In the code you have, you don't need to close the connection - it will
be done by QueryRunner.run method.
You do need to close the connection, when you pass connection object
while running the query,
ResultSetHandler h = ... // Define a handler the same as above example
// No DataSource so we must handle Connections manually
QueryRunner run = new QueryRunner();
Connection conn = ... // open a connection
try{
Object[] result = (Object[]) run.query(
conn, "SELECT * FROM Person WHERE name=?", "John Doe", h);
// do something with the result
} finally {
// Use this helper method so we don't have to check for null
DbUtils.close(conn);
}