commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Tonhofer, m-plify S.A." <>
Subject Some notes on DBCP Connection validation
Date Fri, 17 Sep 2004 14:31:47 GMT

I have been checking the DBCP & Pool source for a couple of hours to
trace Connection validation. Here are the notes, I wanted to
put them into the DBCP Wiki, but it seems the page is immutable?

Have fun,

-- David

Notes on:

Validation of Connections

Q: What is 'Validation of Connections'?

   org.apache.dbcp.PoolableConnectionFactory implements method
   org.apache.commons.pool.impl.GenericObjectPool.validate(Object obj),
   whose description is:

   "Ensures that the instance is safe to be returned by the pool.
    Returns false if this object should be destroyed."

   The implementation of PoolableConnectionFactory.validateObject is as follows:

    public boolean validateObject(Object obj) {
        if(obj instanceof Connection) {
            try {
                validateConnection((Connection) obj);
                return true;
            } catch(Exception e) {
                return false;
        } else {
            return false;

   The implementation of PoolableConnectionFactory.validateConnection
   tries to run the 'validationQuery' passed in the constructor. If this
   fails, it throws an SQLException (and validateObject will return false)

Q: When are Connections validated, or in other words, when you have a
   PoolableConnectionFactory, when is its validateObject() method called
   on a Connection?

   This depends on the pool implementation that you are using

   Case 1: Your Pool is a org.apache.commons.pool.impl.StackObjectPool

   Connections are NOT validated when you borrow them from the pool. They
   are only validated - unconditionally - in returnObject(), which is
   called by addObject(). In other words, they are only validated if they
   are put (back) into the pool.

   The consequence of this is that you will get Exceptions if you
   borrow a stale Connection because immediately after the Object has been
   pop()-ed from the stack, the factory's activateObject() is called on
   the Connection and the first thing it does is set the autocommit default
   value. This will throw on a stale Connection.

   Case 2: Your Pool is a org.apache.commons.pool.impl.GenericObjectPool

   Connections are validated in the following three GenericObjectPool methods:
   evict(), borrowObject() and addObjectToPool(). Thus, GenericObjectPool gives
   you the possibility to also validate the Connections at 'borrow' time.

     Only calls validateObject() if 'testOnBorrow' has been set. This parameter
     is passed in the pool's constructor.
     If validation fails the Connection has not been newly created, Connection is
     discarded and a new one is created in its place.
     Note that if the database goes away, you will see Exceptions coming out of
     borrowObject() as newly created Connections will fail their validation.

     This method is called by the 'Evictor' which clears out idle Objects in the
     pool. It unconditionally runs validateObject() on a pooled Object. If that method
     returns false, the pooled Object is removed.

     Only calls validateObject() if 'testOnReturn' has been set. This parameter
     is passed in the pool's constructor. If the (returned) Object fails validation,
     it is immediately destroyed.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message