Class CPDSConnectionFactory

  • All Implemented Interfaces:
    java.util.EventListener, javax.sql.ConnectionEventListener, PooledConnectionManager, org.apache.commons.pool.PoolableObjectFactory

    class CPDSConnectionFactory
    extends java.lang.Object
    implements org.apache.commons.pool.PoolableObjectFactory, javax.sql.ConnectionEventListener, PooledConnectionManager
    A PoolableObjectFactory that creates PoolableConnections.
    Version:
    $Revision: 907288 $ $Date: 2010-02-06 14:42:58 -0500 (Sat, 06 Feb 2010) $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private javax.sql.ConnectionPoolDataSource _cpds  
      private java.lang.String _password  
      private org.apache.commons.pool.ObjectPool _pool  
      private boolean _rollbackAfterValidation  
      private java.lang.String _username  
      private java.lang.String _validationQuery  
      private static java.lang.String NO_KEY_MESSAGE  
      private java.util.WeakHashMap pcMap
      Map of PooledConnectionAndInfo instances
      private java.util.Map validatingMap
      Map of PooledConnections for which close events are ignored.
    • Constructor Summary

      Constructors 
      Constructor Description
      CPDSConnectionFactory​(javax.sql.ConnectionPoolDataSource cpds, org.apache.commons.pool.ObjectPool pool, java.lang.String validationQuery, boolean rollbackAfterValidation, java.lang.String username, java.lang.String password)
      Create a new PoolableConnectionFactory.
      CPDSConnectionFactory​(javax.sql.ConnectionPoolDataSource cpds, org.apache.commons.pool.ObjectPool pool, java.lang.String validationQuery, java.lang.String username, java.lang.String password)
      Create a new PoolableConnectionFactory.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void activateObject​(java.lang.Object obj)  
      void closePool​(java.lang.String username)
      Verifies that the username matches the user whose connections are being managed by this factory and closes the pool if this is the case; otherwise does nothing.
      void connectionClosed​(javax.sql.ConnectionEvent event)
      This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the user calls the close() method of this connection object.
      void connectionErrorOccurred​(javax.sql.ConnectionEvent event)
      If a fatal error occurs, close the underlying physical connection so as not to be returned in the future
      void destroyObject​(java.lang.Object obj)
      Closes the PooledConnection and stops listening for events from it.
      org.apache.commons.pool.ObjectPool getPool()
      Returns the object pool used to pool connections created by this factory.
      void invalidate​(javax.sql.PooledConnection pc)
      Invalidates the PooledConnection in the pool.
      java.lang.Object makeObject()  
      void passivateObject​(java.lang.Object obj)  
      void setPassword​(java.lang.String password)
      Sets the database password used when creating new connections.
      boolean validateObject​(java.lang.Object obj)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • _cpds

        private final javax.sql.ConnectionPoolDataSource _cpds
      • _validationQuery

        private final java.lang.String _validationQuery
      • _rollbackAfterValidation

        private final boolean _rollbackAfterValidation
      • _pool

        private final org.apache.commons.pool.ObjectPool _pool
      • _username

        private java.lang.String _username
      • _password

        private java.lang.String _password
      • validatingMap

        private final java.util.Map validatingMap
        Map of PooledConnections for which close events are ignored. Connections are muted when they are being validated.
      • pcMap

        private final java.util.WeakHashMap pcMap
        Map of PooledConnectionAndInfo instances
    • Constructor Detail

      • CPDSConnectionFactory

        public CPDSConnectionFactory​(javax.sql.ConnectionPoolDataSource cpds,
                                     org.apache.commons.pool.ObjectPool pool,
                                     java.lang.String validationQuery,
                                     java.lang.String username,
                                     java.lang.String password)
        Create a new PoolableConnectionFactory.
        Parameters:
        cpds - the ConnectionPoolDataSource from which to obtain PooledConnection's
        pool - the ObjectPool in which to pool those Connections
        validationQuery - a query to use to validate Connections. Should return at least one row. May be null
        username -
        password -
      • CPDSConnectionFactory

        public CPDSConnectionFactory​(javax.sql.ConnectionPoolDataSource cpds,
                                     org.apache.commons.pool.ObjectPool pool,
                                     java.lang.String validationQuery,
                                     boolean rollbackAfterValidation,
                                     java.lang.String username,
                                     java.lang.String password)
        Create a new PoolableConnectionFactory.
        Parameters:
        cpds - the ConnectionPoolDataSource from which to obtain PooledConnection's
        pool - the ObjectPool in which to pool those Connections
        validationQuery - a query to use to validate Connections. Should return at least one row. May be null
        rollbackAfterValidation - whether a rollback should be issued after validating Connections.
        username -
        password -
    • Method Detail

      • getPool

        public org.apache.commons.pool.ObjectPool getPool()
        Returns the object pool used to pool connections created by this factory.
        Returns:
        ObjectPool managing pooled connections
      • makeObject

        public java.lang.Object makeObject()
        Specified by:
        makeObject in interface org.apache.commons.pool.PoolableObjectFactory
      • destroyObject

        public void destroyObject​(java.lang.Object obj)
                           throws java.lang.Exception
        Closes the PooledConnection and stops listening for events from it.
        Specified by:
        destroyObject in interface org.apache.commons.pool.PoolableObjectFactory
        Throws:
        java.lang.Exception
      • validateObject

        public boolean validateObject​(java.lang.Object obj)
        Specified by:
        validateObject in interface org.apache.commons.pool.PoolableObjectFactory
      • passivateObject

        public void passivateObject​(java.lang.Object obj)
        Specified by:
        passivateObject in interface org.apache.commons.pool.PoolableObjectFactory
      • activateObject

        public void activateObject​(java.lang.Object obj)
        Specified by:
        activateObject in interface org.apache.commons.pool.PoolableObjectFactory
      • connectionClosed

        public void connectionClosed​(javax.sql.ConnectionEvent event)
        This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the user calls the close() method of this connection object. What we need to do here is to release this PooledConnection from our pool...
        Specified by:
        connectionClosed in interface javax.sql.ConnectionEventListener
      • connectionErrorOccurred

        public void connectionErrorOccurred​(javax.sql.ConnectionEvent event)
        If a fatal error occurs, close the underlying physical connection so as not to be returned in the future
        Specified by:
        connectionErrorOccurred in interface javax.sql.ConnectionEventListener
      • invalidate

        public void invalidate​(javax.sql.PooledConnection pc)
                        throws java.sql.SQLException
        Invalidates the PooledConnection in the pool. The CPDSConnectionFactory closes the connection and pool counters are updated appropriately. Also closes the pool. This ensures that all idle connections are closed and connections that are checked out are closed on return.
        Specified by:
        invalidate in interface PooledConnectionManager
        Parameters:
        pc - PooledConnection to be invalidated
        Throws:
        java.sql.SQLException - if an SQL error occurs closing the connection
      • setPassword

        public void setPassword​(java.lang.String password)
        Sets the database password used when creating new connections.
        Specified by:
        setPassword in interface PooledConnectionManager
        Parameters:
        password - new password
      • closePool

        public void closePool​(java.lang.String username)
                       throws java.sql.SQLException
        Verifies that the username matches the user whose connections are being managed by this factory and closes the pool if this is the case; otherwise does nothing.
        Specified by:
        closePool in interface PooledConnectionManager
        Parameters:
        username - user name
        Throws:
        java.sql.SQLException - if an error occurs closing idle connections in the pool