Class KeyedCPDSConnectionFactory

java.lang.Object
org.apache.commons.dbcp2.datasources.KeyedCPDSConnectionFactory
All Implemented Interfaces:
EventListener, ConnectionEventListener, PooledConnectionManager, org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>

final class KeyedCPDSConnectionFactory extends Object implements org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>, ConnectionEventListener, PooledConnectionManager
A KeyedPooledObjectFactory that creates PoolableConnections.
Since:
2.0
  • Field Details

  • Constructor Details

    • KeyedCPDSConnectionFactory

      public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutSeconds, boolean rollbackAfterValidation)
      Creates a new KeyedPoolableConnectionFactory.
      Parameters:
      cpds - the ConnectionPoolDataSource from which to obtain PooledConnections
      validationQuery - a query to use to validate Connections. Should return at least one row. May be null in which case3 Connection.isValid(int) will be used to validate connections.
      validationQueryTimeoutSeconds - The Duration to allow for the validation query to complete
      rollbackAfterValidation - whether a rollback should be issued after validating Connections.
      Since:
      2.10.0
    • KeyedCPDSConnectionFactory

      @Deprecated public KeyedCPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation)
      Creates a new KeyedPoolableConnectionFactory.
      Parameters:
      cpds - the ConnectionPoolDataSource from which to obtain PooledConnections
      validationQuery - a query to use to validate Connections. Should return at least one row. May be null in which case3 Connection.isValid(int) will be used to validate connections.
      validationQueryTimeoutSeconds - The time, in seconds, to allow for the validation query to complete
      rollbackAfterValidation - whether a rollback should be issued after validating Connections.
  • Method Details

    • activateObject

      public void activateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException
      Specified by:
      activateObject in interface org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>
      Throws:
      SQLException
    • closePool

      public void closePool(String userName) throws 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:
      SQLException - if an error occurs closing idle connections in the pool
    • connectionClosed

      public void connectionClosed(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 ConnectionEventListener
    • connectionErrorOccurred

      public void connectionErrorOccurred(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 ConnectionEventListener
    • destroyObject

      public void destroyObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException
      Closes the PooledConnection and stops listening for events from it.
      Specified by:
      destroyObject in interface org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>
      Throws:
      SQLException
    • getPool

      public org.apache.commons.pool2.KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> getPool()
      Returns the keyed object pool used to pool connections created by this factory.
      Returns:
      KeyedObjectPool managing pooled connections
    • invalidate

      public void invalidate(PooledConnection pc) throws 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 user name 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:
      SQLException - if an SQL error occurs closing the connection
    • makeObject

      public org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> makeObject(UserPassKey userPassKey) throws SQLException
      Creates a new PooledConnectionAndInfo from the given UserPassKey.
      Specified by:
      makeObject in interface org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>
      Parameters:
      userPassKey - UserPassKey containing user credentials
      Throws:
      SQLException - if the connection could not be created.
      See Also:
      • KeyedPooledObjectFactory.makeObject(Object)
    • passivateObject

      public void passivateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p) throws SQLException
      Specified by:
      passivateObject in interface org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>
      Throws:
      SQLException
    • setMaxConn

      public void setMaxConn(Duration maxConnLifetimeMillis)
      Sets the maximum lifetime of a connection after which the connection will always fail activation, passivation and validation.
      Parameters:
      maxConnLifetimeMillis - A value of zero or less indicates an infinite lifetime. The default value is -1 milliseconds.
      Since:
      2.10.0
    • setMaxConnLifetime

      @Deprecated public void setMaxConnLifetime(Duration maxConnLifetimeMillis)
      Deprecated.
      Sets the maximum lifetime of a connection after which the connection will always fail activation, passivation and validation.
      Parameters:
      maxConnLifetimeMillis - A value of zero or less indicates an infinite lifetime. The default value is -1 milliseconds.
      Since:
      2.9.0
    • setMaxConnLifetimeMillis

      @Deprecated public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
      Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.
      Parameters:
      maxConnLifetimeMillis - A value of zero or less indicates an infinite lifetime. The default value is -1.
    • setPassword

      public void setPassword(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
    • setPool

      public void setPool(org.apache.commons.pool2.KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> pool)
    • validateLifetime

      private void validateLifetime(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> pooledObject) throws SQLException
      Throws:
      SQLException
    • validateObject

      public boolean validateObject(UserPassKey key, org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> pooledObject)
      Validates a pooled connection.
      Specified by:
      validateObject in interface org.apache.commons.pool2.KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>
      Parameters:
      key - ignored
      pooledObject - wrapped PooledConnectionAndInfo containing the connection to validate
      Returns:
      true if validation succeeds