commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Graham <>
Subject Re: [DbUtils] close connection in client code ?
Date Sat, 13 Dec 2003 03:37:53 GMT

--- John Zoetebier <> wrote:
> The web site of DbUtils says:
> ==>
> DbUtils is a small set of classes designed to make working with JDBC 
> easier. JDBC resource cleanup code is mundane, error prone work so these
> classes abstract out all of the cleanup tasks from your code leaving you
> with what you really wanted to do with JDBC in the first place: query
> and 
> update data.
> ==>
> Initially I though DbUtils would closes the connection as well, but this
> does not seem to be the case.

DbUtils does properly close all connections it retrieves from a

> This is probably to simplify coding and to increase performance.
> My conclusion is that code using DbUtils is responsible for closing a 
> DataSource or Connection.
> This is not mentioned on the web site.

Yes it is, check the QueryRunner javadocs.  QueryRunner can operate with
either a DataSource or by passing a Connection into one of its query()
methods.  DbUtils is not responsible for closing a Connection that you
pass in manually.

> It looks like a connection should be closed with
> DbUtils.close(Connection 
> connection)
> The advantage of this method is that it handles a null connection.

You can use this method to close your Connection objects after DbUtils is
done with them.

> There is no method to close a DataSource.

How do you close a DataSource?

> How do I have to handle a DataSource when working with DbUtils?

Pass the DataSource into a QueryRunner constructor and DbUtils handles the
rest.  That's the nice part about using DataSources instead of handling
Connections yourself.  The Connection functionality is there to allow you
to create arbitrarily complex transaction logic without DbUtils getting in
the way.  For simple queries and updates it's better to use a DataSource.


> -- 
> John Zoetebier
> Web site:

Do you Yahoo!?
Free Pop-Up Blocker - Get it now

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message