commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andy Clark" <acl...@seven.com>
Subject [dbcp] Dealing with DB connection errors
Date Mon, 19 Mar 2007 17:07:21 GMT
Hi

I'm using DBCP's SharedPoolDataSource with the PostgreSQL
PGConnectionPoolDataSource, and whenever I get a connection error from
the DB, the connection doesn't seem to be released back to the pool. I
am trying to figure out if there's a leak here, or if I'm just not doing
things in the right way.

If there's some DB connection failure while the Connection is 'active'
(i.e. borrowed from the pool), then:
 * the
KeyedCPDSConnectionFactory.connectionErrorOccurred(ConnectionEvent)
handler is fired. 
 * I get "CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR" on stderr, 
 * the KeyedCPDSConnectionFactory stops listening for further connection
events on this pooled connection, 
 * PooledConnection.close() is called to close the underlying DB
connection. 

However, the pooled connection is not returned to the pool here, as far
as I can make out, so my pool still thinks I have one more connection
active than I actually do.

The Exception reporting the disconnect error makes its way up to the
application layer, where it causes a finally block to call
Connection.close(). So I would then expect the connection to be returned
to the pool by
KeyedCPDSConnectionFactory.connectionClosed(ConnectionEvent), since this
looks like the only place where we return connections to the pool. 

However, this event is not fired when the application calls
connection.close(), since the connectionErrorOccurred handler has
already closed the underlying connection. And if the underlying
connection weren't already closed at this stage, then it still wouldn't
get picked up since the connectionErrorOccurred handler caused the
KeyedCPDSConnectionFactory to unregister for events on that connection.

This leaves me with a case where the pool thinks all my connections are
active, when in fact they're all dead due to connection errors. However,
no more connections can be created because the pool is at its limit of
active connections. Is this what is expected? If so, how should I detect
and recover the dead connections?

I have attached a class which should demonstrate the problem - it needs
the following to run:

commons-dbcp-1.2.1
commons-pool-1.3
commons-collections-2.1
postgresql-8.1-405.jdbc3.jar
log4j

A PostgreSQL server :-)

I hope I have included the right information here. Please let me know if
there is anything else you need.

Thanks, 
Andy

-- 
Andy Clark
aclark@seven.com




Mime
View raw message