On 12/22/16 9:34 AM, Shawn Heisey wrote:
> On 12/22/2016 4:49 AM, 王钦 wrote:
>>     When I use DBCP-1.4 in my work, I need to close the connection
>> which is generated by BasicDataSource. How can I do it? Close the
>> connection, while not returning it back to the Connection Pool. 
> The PoolableConnection class (which I believe is the actual type of
> object you will receive from the data source) has a "reallyClose()" method.
>
> http://commons.apache.org/proper/commons-dbcp/api-1.4/org/apache/commons/dbcp/PoolableConnection.html#reallyClose()
>
> I'm no expert, but I think you might be able to cast the connection
> received from BasicDataSource to the Poolable variety.
>
> If you intend to "reallyClose()" every connection (rather than just some
> of them), I do have to wonder why you're using DBCP at all.  Obtaining
> connections from JDBC directly and closing them normally would
> accomplish the same thing without a code dependency and slightly less
> overhead.

I agree with your statement about defeating the purpose of the pool
if you do this every time.  If done only when the client knows the
connection is bad, the solution above will unfortunately leak pool
capacity (reallyClose doesn't inform the pool that the checked out
connection is never coming back).  A sort of ugly workaround for
that would be to enable abandoned connection cleanup, which would
eventually clean these up. 

What you need to do to do this cleanly is to get the underlying
GenericObjectPool to invalidate the PoolableConnection so its
accounting is correctly updated.  If you are willing to upgrade to
DBCP 2, BasicDataSource now has an invalidateConnection method that
handles this.  If not, the only way to do this without leaking
capacity is to do what the source for that method does, which
requires that you get a reference to the underlying object pool. 
BasicDatasource does not expose that, so if you want to go this
route, you need to manually create the GOP and use a PoolingDatasource.

Phil
>
> Thanks,
> Shawn
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to