Class CPDSConnectionFactory

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

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

  • Constructor Details

    • CPDSConnectionFactory

      public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutDuration, boolean rollbackAfterValidation, String userName, char[] userPassword)
      Creates a new PoolableConnectionFactory.
      Parameters:
      cpds - the ConnectionPoolDataSource from which to obtain PooledConnection's
      validationQuery - a query to use to validate Connections. Should return at least one row. May be null in which case Connection.isValid(int) will be used to validate connections.
      validationQueryTimeoutDuration - Timeout Duration before validation fails
      rollbackAfterValidation - whether a rollback should be issued after validating Connections.
      userName - The user name to use to create connections
      userPassword - The password to use to create connections
      Since:
      2.10.0
    • CPDSConnectionFactory

      public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, Duration validationQueryTimeoutDuration, boolean rollbackAfterValidation, String userName, String userPassword)
      Creates a new PoolableConnectionFactory.
      Parameters:
      cpds - the ConnectionPoolDataSource from which to obtain PooledConnection's
      validationQuery - a query to use to validate Connections. Should return at least one row. May be null in which case Connection.isValid(int) will be used to validate connections.
      validationQueryTimeoutDuration - Timeout in seconds before validation fails
      rollbackAfterValidation - whether a rollback should be issued after validating Connections.
      userName - The user name to use to create connections
      userPassword - The password to use to create connections
      Since:
      2.10.0
    • CPDSConnectionFactory

      @Deprecated public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, char[] userPassword)
      Creates a new PoolableConnectionFactory.
      Parameters:
      cpds - the ConnectionPoolDataSource from which to obtain PooledConnection's
      validationQuery - a query to use to validate Connections. Should return at least one row. May be null in which case Connection.isValid(int) will be used to validate connections.
      validationQueryTimeoutSeconds - Timeout in seconds before validation fails
      rollbackAfterValidation - whether a rollback should be issued after validating Connections.
      userName - The user name to use to create connections
      userPassword - The password to use to create connections
      Since:
      2.4.0
    • CPDSConnectionFactory

      @Deprecated public CPDSConnectionFactory(ConnectionPoolDataSource cpds, String validationQuery, int validationQueryTimeoutSeconds, boolean rollbackAfterValidation, String userName, String userPassword)
      Creates a new PoolableConnectionFactory.
      Parameters:
      cpds - the ConnectionPoolDataSource from which to obtain PooledConnection's
      validationQuery - a query to use to validate Connections. Should return at least one row. May be null in which case Connection.isValid(int) will be used to validate connections.
      validationQueryTimeoutSeconds - Timeout in seconds before validation fails
      rollbackAfterValidation - whether a rollback should be issued after validating Connections.
      userName - The user name to use to create connections
      userPassword - The password to use to create connections
  • Method Details

    • activateObject

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

      public void closePool(String userName) throws SQLException
      Verifies that the user name 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:
      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(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.PooledObjectFactory<PooledConnectionAndInfo>
      Throws:
      SQLException
    • doDestroyObject

      private void doDestroyObject(PooledConnectionAndInfo pci) throws SQLException
      Throws:
      SQLException
    • getPasswordCharArray

      char[] getPasswordCharArray()
      (Testing API) Gets the value of password for the default user.
      Returns:
      value of password.
    • getPool

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

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

      public org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> makeObject() throws SQLException
      Specified by:
      makeObject in interface org.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>
      Throws:
      SQLException
    • passivateObject

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

      public void setMaxConn(Duration maxConnDuration)
      Sets the maximum Duration of a connection after which the connection will always fail activation, passivation and validation.
      Parameters:
      maxConnDuration - 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 maxConnDuration)
      Deprecated.
      Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.
      Parameters:
      maxConnDuration - 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)
      Deprecated.
      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(char[] userPassword)
      Sets the database password used when creating new connections.
      Parameters:
      userPassword - new password
    • setPassword

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

      public void setPool(org.apache.commons.pool2.ObjectPool<PooledConnectionAndInfo> pool)
      Parameters:
      pool - the ObjectPool in which to pool those Connections
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Since:
      2.6.0
    • validateLifetime

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

      public boolean validateObject(org.apache.commons.pool2.PooledObject<PooledConnectionAndInfo> p)
      Specified by:
      validateObject in interface org.apache.commons.pool2.PooledObjectFactory<PooledConnectionAndInfo>