commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Phil Steitz" <phil.ste...@gmail.com>
Subject Re: [DBCP] Getting connections from PoolingDataSource
Date Fri, 11 May 2007 05:20:32 GMT
On 5/10/07, Tushar Madhukar <tushar.madhukar@gmail.com> wrote:
> Phil/Sayeed, appreciate your help.
>
> Thanks.
>
> On 5/11/07, Phil Steitz <phil.steitz@gmail.com> wrote:
> > On 5/10/07, Saeed <sayeed_4932@yahoo.com> wrote:
> > > my comments are in red.
> > >
> > > Tushar Madhukar <tushar.madhukar@gmail.com> wrote:    Hi,
> > >
> > > I am using DBCP (PoolingDataSource with GenericObjectPool) for pooling
> > > database connections.
> > >
> > > 1. My web application can have multiple clients accessing connections
> > > simultaneously. Is PoolingDataSource.getConnection() thread safe or do
> > > I have to handle synchronization?
> > > yes, it is thread safe.
> >
> > Technically, this is only true if the underlying pool's borrowObject
> > is threadsafe.  If you use commons pool's GenericObjectPool (as dbcp's
> > BasicDataSource does), this is true.  Also, if you allow multiple
> > threads to access the connections returned by getConnection, it is up
> > to you to synchronize access while they are checked out of the pool.
> > >
> > > 2. How to release all connections in the pool, when the application closes?
> > > conn.close();
> >
> > If conn is a connection returned by getConnection, that will only
> > return it to the pool.  To close all connections in the pool, you need
> > to close the pool or datasource itself. BasicDataSource has a close
> > method that does this.  If you are using PoolingDataSource directly,
> > you need to hang on to a reference to the pool that you used to create
> > it and invoke the close method on the pool.
> >
One more point on this.  While in many cases, gc will take care of
this, to be certain that all connections that you open via the pool
get promptly closed, you need to return them all to the pool before
closing the pool, since BasicDataSource.close only closes the
connections that have been returned to the pool.

Phil

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