commons-user mailing list archives

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

--- John Zoetebier <john.zoetebier@transparent.co.nz> 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
DataSource.

> 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.

David


> 
> -- 
> John Zoetebier
> Web site: http://www.transparent.co.nz


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message