Class PooledConnectionImpl

java.lang.Object
org.datanucleus.store.rdbms.datasource.dbcp2.cpdsadapter.PooledConnectionImpl
All Implemented Interfaces:
PooledConnection, KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement>

Implementation of PooledConnection that is returned by PooledConnectionDataSource.
Since:
2.0
  • Field Details

    • CLOSED

      private static final String CLOSED
      See Also:
    • connection

      private Connection connection
      The JDBC database connection that represents the physical db connection.
    • delegatingConnection

      private final DelegatingConnection<?> delegatingConnection
      A DelegatingConnection used to create a PoolablePreparedStatementStub.
    • logicalConnection

      private Connection logicalConnection
      The JDBC database logical connection.
    • eventListeners

      private final Vector<ConnectionEventListener> eventListeners
      ConnectionEventListeners.
    • statementEventListeners

      private final Vector<StatementEventListener> statementEventListeners
      StatementEventListeners.
    • closed

      private boolean closed
      Flag set to true, once close() is called.
    • pStmtPool

    • accessToUnderlyingConnectionAllowed

      private boolean accessToUnderlyingConnectionAllowed
      Controls access to the underlying connection.
  • Constructor Details

    • PooledConnectionImpl

      PooledConnectionImpl(Connection connection)
      Wraps the real connection.
      Parameters:
      connection - the connection to be wrapped.
  • Method Details

    • activateObject

      public void activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws Exception
      My KeyedPooledObjectFactory method for activating PreparedStatements.
      Specified by:
      activateObject in interface KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement>
      Parameters:
      key - Ignored.
      pooledObject - Ignored.
      Throws:
      Exception - if there is a problem activating obj, this exception may be swallowed by the pool.
      See Also:
    • addConnectionEventListener

      public void addConnectionEventListener(ConnectionEventListener listener)
      Specified by:
      addConnectionEventListener in interface PooledConnection
    • addStatementEventListener

      public void addStatementEventListener(StatementEventListener listener)
      Specified by:
      addStatementEventListener in interface PooledConnection
    • assertOpen

      private void assertOpen() throws SQLException
      Throws an SQLException, if isClosed is true
      Throws:
      SQLException
    • close

      public void close() throws SQLException
      Closes the physical connection and marks this PooledConnection so that it may not be used to generate any more logical Connections.
      Specified by:
      close in interface PooledConnection
      Throws:
      SQLException - Thrown when an error occurs or the connection is already closed.
    • createKey

      protected PStmtKey createKey(String sql)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      Returns:
      a PStmtKey for the given arguments.
    • createKey

      protected PStmtKey createKey(String sql, int autoGeneratedKeys)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      autoGeneratedKeys - A flag indicating whether auto-generated keys should be returned; one of Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS.
      Returns:
      a key to uniquely identify a prepared statement.
    • createKey

      protected PStmtKey createKey(String sql, int[] columnIndexes)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      columnIndexes - An array of column indexes indicating the columns that should be returned from the inserted row or rows.
      Returns:
      a key to uniquely identify a prepared statement.
    • createKey

      protected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      resultSetType - A result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE.
      resultSetConcurrency - A concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE.
      Returns:
      a key to uniquely identify a prepared statement.
    • createKey

      protected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE.
      resultSetConcurrency - A concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
      resultSetHoldability - One of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT.
      Returns:
      a key to uniquely identify a prepared statement.
    • createKey

      protected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, PoolingConnection.StatementType statementType)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
      resultSetConcurrency - A concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE.
      resultSetHoldability - One of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT.
      statementType - The SQL statement type, prepared or callable.
      Returns:
      a key to uniquely identify a prepared statement.
      Since:
      2.4.0
    • createKey

      protected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType statementType)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      resultSetType - A result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE.
      resultSetConcurrency - A concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE.
      statementType - The SQL statement type, prepared or callable.
      Returns:
      a key to uniquely identify a prepared statement.
      Since:
      2.4.0
    • createKey

      protected PStmtKey createKey(String sql, PoolingConnection.StatementType statementType)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      statementType - The SQL statement type, prepared or callable.
      Returns:
      a key to uniquely identify a prepared statement.
    • createKey

      protected PStmtKey createKey(String sql, String[] columnNames)
      Creates a PStmtKey for the given arguments.
      Parameters:
      sql - The SQL statement.
      columnNames - An array of column names indicating the columns that should be returned from the inserted row or rows.
      Returns:
      a key to uniquely identify a prepared statement.
    • destroyObject

      public void destroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws Exception
      My KeyedPooledObjectFactory method for destroying PreparedStatements.
      Specified by:
      destroyObject in interface KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement>
      Parameters:
      key - ignored
      pooledObject - the wrapped PreparedStatement to be destroyed.
      Throws:
      Exception - should be avoided as it may be swallowed by the pool implementation.
      See Also:
    • finalize

      protected void finalize() throws Throwable
      Closes the physical connection and checks that the logical connection was closed as well.
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • getCatalogOrNull

      private String getCatalogOrNull()
    • getSchemaOrNull

      private String getSchemaOrNull()
    • getConnection

      public Connection getConnection() throws SQLException
      Returns a JDBC connection.
      Specified by:
      getConnection in interface PooledConnection
      Returns:
      The database connection.
      Throws:
      SQLException - if the connection is not open or the previous logical connection is still open
    • isAccessToUnderlyingConnectionAllowed

      public boolean isAccessToUnderlyingConnectionAllowed()
      Returns the value of the accessToUnderlyingConnectionAllowed property.
      Returns:
      true if access to the underlying is allowed, false otherwise.
    • makeObject

      Specified by:
      makeObject in interface KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement>
      Parameters:
      key - The key for the PreparedStatement to be created.
      Returns:
      a PooledObject wrapping an instance that can be served by the pool.
      Throws:
      Exception - if there is a problem creating a new instance, this will be propagated to the code requesting an object.
    • normalizeSQL

      protected String normalizeSQL(String sql)
      Normalizes the given SQL statement, producing a canonical form that is semantically equivalent to the original.
      Parameters:
      sql - The SQL statement.
      Returns:
      the normalized SQL statement.
    • notifyListeners

      void notifyListeners()
      Sends a connectionClosed event.
    • passivateObject

      public void passivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject) throws Exception
      Specified by:
      passivateObject in interface KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement>
      Parameters:
      key - ignored
      pooledObject - a wrapped PreparedStatement
      Throws:
      Exception - if there is a problem passivating obj, this exception may be swallowed by the pool.
      See Also:
    • prepareCall

      CallableStatement prepareCall(String sql) throws SQLException
      Creates or obtains a CallableStatement from my pool.
      Parameters:
      sql - an SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is specified using JDBC call escape syntax.
      Returns:
      a default CallableStatement object containing the pre-compiled SQL statement.
      Throws:
      SQLException - Thrown if a database access error occurs or this method is called on a closed connection.
      Since:
      2.4.0
    • prepareCall

      CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
      Creates or obtains a CallableStatement from my pool.
      Parameters:
      sql - a String object that is the SQL statement to be sent to the database; may contain on or more '?' parameters.
      resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE.
      resultSetConcurrency - a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE.
      Returns:
      a CallableStatement object containing the pre-compiled SQL statement that will produce ResultSet objects with the given type and concurrency.
      Throws:
      SQLException - Thrown if a database access error occurs, this method is called on a closed connection or the given parameters are not ResultSet constants indicating type and concurrency.
      Since:
      2.4.0
    • prepareCall

      CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
      Creates or obtains a CallableStatement from my pool.
      Parameters:
      sql - a String object that is the SQL statement to be sent to the database; may contain on or more '?' parameters.
      resultSetType - one of the following ResultSet constants: ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE.
      resultSetConcurrency - one of the following ResultSet constants: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE.
      resultSetHoldability - one of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT.
      Returns:
      a new CallableStatement object, containing the pre-compiled SQL statement, that will generate ResultSet objects with the given type, concurrency, and holdability.
      Throws:
      SQLException - Thrown if a database access error occurs, this method is called on a closed connection or the given parameters are not ResultSet constants indicating type, concurrency, and holdability.
      Since:
      2.4.0
    • prepareStatement

      PreparedStatement prepareStatement(String sql) throws SQLException
      Creates or obtains a PreparedStatement from my pool.
      Parameters:
      sql - the SQL statement.
      Returns:
      a PoolablePreparedStatement
      Throws:
      SQLException
    • prepareStatement

      PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
      Creates or obtains a PreparedStatement from my pool.
      Parameters:
      sql - an SQL statement that may contain one or more '?' IN parameter placeholders.
      autoGeneratedKeys - a flag indicating whether auto-generated keys should be returned; one of Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS.
      Returns:
      a PoolablePreparedStatement
      Throws:
      SQLException
      See Also:
    • prepareStatement

      PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
      Throws:
      SQLException
    • prepareStatement

      PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
      Creates or obtains a PreparedStatement from my pool.
      Parameters:
      sql - a String object that is the SQL statement to be sent to the database; may contain one or more '?' IN parameters.
      resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE.
      resultSetConcurrency - a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE.
      Returns:
      a PoolablePreparedStatement.
      Throws:
      SQLException
      See Also:
    • prepareStatement

      PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
      Throws:
      SQLException
    • prepareStatement

      PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
      Throws:
      SQLException
    • removeConnectionEventListener

      public void removeConnectionEventListener(ConnectionEventListener listener)
      Specified by:
      removeConnectionEventListener in interface PooledConnection
    • removeStatementEventListener

      public void removeStatementEventListener(StatementEventListener listener)
      Specified by:
      removeStatementEventListener in interface PooledConnection
    • setAccessToUnderlyingConnectionAllowed

      public void setAccessToUnderlyingConnectionAllowed(boolean allow)
      Sets the value of the accessToUnderlyingConnectionAllowed property. It controls if the PoolGuard allows access to the underlying connection. (Default: false.)
      Parameters:
      allow - Access to the underlying connection is granted when true.
    • setStatementPool

      public void setStatementPool(KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> statementPool)
    • validateObject

      public boolean validateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> pooledObject)
      My KeyedPooledObjectFactory method for validating PreparedStatements.
      Specified by:
      validateObject in interface KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement>
      Parameters:
      key - Ignored.
      pooledObject - Ignored.
      Returns:
      true
    • toString

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