Class KeyedCPDSConnectionFactory

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

    class KeyedCPDSConnectionFactory
    extends java.lang.Object
    implements org.apache.commons.pool.KeyedPoolableObjectFactory, javax.sql.ConnectionEventListener, PooledConnectionManager
    A {*link PoolableObjectFactory} that creates {*link PoolableConnection}s.
    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 org.apache.commons.pool.KeyedObjectPool _pool  
      private boolean _rollbackAfterValidation  
      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
      KeyedCPDSConnectionFactory​(javax.sql.ConnectionPoolDataSource cpds, org.apache.commons.pool.KeyedObjectPool pool, java.lang.String validationQuery)
      Create a new KeyedPoolableConnectionFactory.
      KeyedCPDSConnectionFactory​(javax.sql.ConnectionPoolDataSource cpds, org.apache.commons.pool.KeyedObjectPool pool, java.lang.String validationQuery, boolean rollbackAfterValidation)
      Create a new KeyedPoolableConnectionFactory.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void activateObject​(java.lang.Object key, java.lang.Object obj)  
      void closePool​(java.lang.String username)
      This implementation does not fully close the KeyedObjectPool, as this would affect all users.
      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 key, java.lang.Object obj)
      Closes the PooledConnection and stops listening for events from it.
      org.apache.commons.pool.KeyedObjectPool getPool()
      Returns the keyed 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​(java.lang.Object key)
      Creates a new PooledConnectionAndInfo from the given UserPassKey.
      void passivateObject​(java.lang.Object key, java.lang.Object obj)  
      void setPassword​(java.lang.String password)
      Does nothing.
      boolean validateObject​(java.lang.Object key, java.lang.Object obj)
      Validates a pooled connection.
      • 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.KeyedObjectPool _pool
      • 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

      • KeyedCPDSConnectionFactory

        public KeyedCPDSConnectionFactory​(javax.sql.ConnectionPoolDataSource cpds,
                                          org.apache.commons.pool.KeyedObjectPool pool,
                                          java.lang.String validationQuery)
        Create a new KeyedPoolableConnectionFactory.
        Parameters:
        cpds - the ConnectionPoolDataSource from which to obtain PooledConnection's
        pool - the {*link ObjectPool} in which to pool those {*link Connection}s
        validationQuery - a query to use to {*link #validateObject validate} {*link Connection}s. Should return at least one row. May be null
      • KeyedCPDSConnectionFactory

        public KeyedCPDSConnectionFactory​(javax.sql.ConnectionPoolDataSource cpds,
                                          org.apache.commons.pool.KeyedObjectPool pool,
                                          java.lang.String validationQuery,
                                          boolean rollbackAfterValidation)
        Create a new KeyedPoolableConnectionFactory.
        Parameters:
        cpds - the ConnectionPoolDataSource from which to obtain PooledConnections
        pool - the KeyedObjectPool 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.
    • Method Detail

      • getPool

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

        public java.lang.Object makeObject​(java.lang.Object key)
                                    throws java.lang.Exception
        Creates a new PooledConnectionAndInfo from the given UserPassKey.
        Specified by:
        makeObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - UserPassKey containing user credentials
        Throws:
        java.sql.SQLException - if the connection could not be created.
        java.lang.Exception
        See Also:
        KeyedPoolableObjectFactory.makeObject(java.lang.Object)
      • destroyObject

        public void destroyObject​(java.lang.Object key,
                                  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.KeyedPoolableObjectFactory
        Throws:
        java.lang.Exception
      • validateObject

        public boolean validateObject​(java.lang.Object key,
                                      java.lang.Object obj)
        Validates a pooled connection.
        Specified by:
        validateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - ignored
        obj - PooledConnectionAndInfo containing the connection to validate
        Returns:
        true if validation suceeds
      • passivateObject

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

        public void activateObject​(java.lang.Object key,
                                   java.lang.Object obj)
        Specified by:
        activateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
      • 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 KeyedCPDSConnectionFactory closes the connection and pool counters are updated appropriately. Also clears any idle instances associated with the username that was used to create the PooledConnection. Connections associated with this user are not affected and they will not be automatically closed on return to the pool.
        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)
        Does nothing. This factory does not cache user credentials.
        Specified by:
        setPassword in interface PooledConnectionManager
        Parameters:
        password - password used when authenticating to the database
      • closePool

        public void closePool​(java.lang.String username)
                       throws java.sql.SQLException
        This implementation does not fully close the KeyedObjectPool, as this would affect all users. Instead, it clears the pool associated with the given user. This method is not currently used.
        Specified by:
        closePool in interface PooledConnectionManager
        Parameters:
        username - user name
        Throws:
        java.sql.SQLException - if an error occurs closing idle connections in the pool