How to auto reconnect Oracle connection pool after connection interruption

connection-poolingdatabase connectionjdbcoracle10g

I'm running across a problem with Oracle connection pooling via OracleConnectionCacheImpl. When I connect to a connection pool on my database server (Oracle 10g) via the thin driver, everything works fine until after an unspecified time, the db connection gets dropped (possibly due to idle connections?) and I get an error message:

Closed Connection.

Refreshing the page and reattempting the connection reconnects the database, so it does not seem to be a problem with the network or the database. Is a way for the Oracle connection pool to validate its connection and reconnect if the connection in the pool is dead for any reason?

I am using Apache Tomcat 6.0.18, Java EE6 update 11.

(There seems to be a ping function in the Oracle thin driver. Does that help and where can I find it?)

Best Answer

Apache DBCP (DB connection pool) will help here. You can configure with with a 'validationQuery' parameter, which is an SQL query to run on a connection removed from the pool BEFORE it gives it to you. If the query fails, the connection is discarded and a new one is made and given to you.

See here http://commons.apache.org/proper/commons-dbcp/configuration.html for more details.

Note, the validationQuery will be called every time you get a connection from the pool, so it needs to be as fast as possible. eg:

SELECT id FROM users LIMIT 1;
Related Topic