Class PooledConnectionImpl

  • All Implemented Interfaces:
    javax.sql.PooledConnection, org.apache.commons.pool.KeyedPoolableObjectFactory

    class PooledConnectionImpl
    extends java.lang.Object
    implements javax.sql.PooledConnection, org.apache.commons.pool.KeyedPoolableObjectFactory
    Implementation of PooledConnection that is returned by PooledConnectionDataSource.
    Version:
    $Revision: 899987 $ $Date: 2010-01-16 11:51:16 -0500 (Sat, 16 Jan 2010) $
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  PooledConnectionImpl.PStmtKey
      A key uniquely identifying {*link PreparedStatement}s.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean accessToUnderlyingConnectionAllowed
      Controls access to the underlying connection
      private static java.lang.String CLOSED  
      private java.sql.Connection connection
      The JDBC database connection that represents the physical db connection.
      private DelegatingConnection delegatingConnection
      A DelegatingConnection used to create a PoolablePreparedStatementStub
      private java.util.Vector eventListeners
      ConnectionEventListeners
      (package private) boolean isClosed
      flag set to true, once close() is called.
      private java.sql.Connection logicalConnection
      The JDBC database logical connection.
      protected org.apache.commons.pool.KeyedObjectPool pstmtPool
      My pool of {*link PreparedStatement}s.
      private java.util.Vector statementEventListeners
      StatementEventListeners
    • Constructor Summary

      Constructors 
      Constructor Description
      PooledConnectionImpl​(java.sql.Connection connection, org.apache.commons.pool.KeyedObjectPool pool)
      Wrap the real connection.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void activateObject​(java.lang.Object key, java.lang.Object obj)
      My {*link KeyedPoolableObjectFactory} method for activating {*link PreparedStatement}s.
      void addConnectionEventListener​(javax.sql.ConnectionEventListener listener)
      void addStatementEventListener​(javax.sql.StatementEventListener listener)  
      private void assertOpen()
      Throws an SQLException, if isClosed is true
      void close()
      Closes the physical connection and marks this PooledConnection so that it may not be used to generate any more logical Connections.
      protected java.lang.Object createKey​(java.lang.String sql)
      Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      protected java.lang.Object createKey​(java.lang.String sql, int autoGeneratedKeys)
      Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      protected java.lang.Object createKey​(java.lang.String sql, int[] columnIndexes)
      Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      protected java.lang.Object createKey​(java.lang.String sql, int resultSetType, int resultSetConcurrency)
      Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      protected java.lang.Object createKey​(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
      Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      protected java.lang.Object createKey​(java.lang.String sql, java.lang.String[] columnNames)
      Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      void destroyObject​(java.lang.Object key, java.lang.Object obj)
      My {*link KeyedPoolableObjectFactory} method for destroying {*link PreparedStatement}s.
      protected void finalize()
      Closes the physical connection and checks that the logical connection was closed as well.
      java.sql.Connection getConnection()
      Returns a JDBC connection.
      boolean isAccessToUnderlyingConnectionAllowed()
      Returns the value of the accessToUnderlyingConnectionAllowed property.
      java.lang.Object makeObject​(java.lang.Object obj)
      My {*link KeyedPoolableObjectFactory} method for creating {*link PreparedStatement}s.
      protected java.lang.String normalizeSQL​(java.lang.String sql)
      Normalize the given SQL statement, producing a cannonical form that is semantically equivalent to the original.
      (package private) void notifyListeners()
      sends a connectionClosed event.
      void passivateObject​(java.lang.Object key, java.lang.Object obj)
      My {*link KeyedPoolableObjectFactory} method for passivating {*link PreparedStatement}s.
      (package private) java.sql.PreparedStatement prepareStatement​(java.lang.String sql)
      Create or obtain a PreparedStatement from my pool.
      (package private) java.sql.PreparedStatement prepareStatement​(java.lang.String sql, int autoGeneratedKeys)
      Create or obtain a PreparedStatement from my pool.
      (package private) java.sql.PreparedStatement prepareStatement​(java.lang.String sql, int[] columnIndexes)  
      (package private) java.sql.PreparedStatement prepareStatement​(java.lang.String sql, int resultSetType, int resultSetConcurrency)
      Create or obtain a PreparedStatement from my pool.
      (package private) java.sql.PreparedStatement prepareStatement​(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)  
      (package private) java.sql.PreparedStatement prepareStatement​(java.lang.String sql, java.lang.String[] columnNames)  
      void removeConnectionEventListener​(javax.sql.ConnectionEventListener listener)
      void removeStatementEventListener​(javax.sql.StatementEventListener listener)  
      void setAccessToUnderlyingConnectionAllowed​(boolean allow)
      Sets the value of the accessToUnderlyingConnectionAllowed property.
      boolean validateObject​(java.lang.Object key, java.lang.Object obj)
      My {*link KeyedPoolableObjectFactory} method for validating {*link PreparedStatement}s.
      • Methods inherited from class java.lang.Object

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

      • connection

        private java.sql.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 java.sql.Connection logicalConnection
        The JDBC database logical connection.
      • eventListeners

        private final java.util.Vector eventListeners
        ConnectionEventListeners
      • statementEventListeners

        private final java.util.Vector statementEventListeners
        StatementEventListeners
      • isClosed

        boolean isClosed
        flag set to true, once close() is called.
      • pstmtPool

        protected org.apache.commons.pool.KeyedObjectPool pstmtPool
        My pool of {*link PreparedStatement}s.
      • accessToUnderlyingConnectionAllowed

        private boolean accessToUnderlyingConnectionAllowed
        Controls access to the underlying connection
    • Constructor Detail

      • PooledConnectionImpl

        PooledConnectionImpl​(java.sql.Connection connection,
                             org.apache.commons.pool.KeyedObjectPool pool)
        Wrap the real connection.
        Parameters:
        connection - the connection to be wrapped
        pool - the pool to use
    • Method Detail

      • addConnectionEventListener

        public void addConnectionEventListener​(javax.sql.ConnectionEventListener listener)
        Specified by:
        addConnectionEventListener in interface javax.sql.PooledConnection
      • addStatementEventListener

        public void addStatementEventListener​(javax.sql.StatementEventListener listener)
        Specified by:
        addStatementEventListener in interface javax.sql.PooledConnection
      • close

        public void close()
                   throws java.sql.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 javax.sql.PooledConnection
        Throws:
        java.sql.SQLException - if an error occurs or the connection is already closed
      • assertOpen

        private void assertOpen()
                         throws java.sql.SQLException
        Throws an SQLException, if isClosed is true
        Throws:
        java.sql.SQLException
      • getConnection

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

        public void removeConnectionEventListener​(javax.sql.ConnectionEventListener listener)
        Specified by:
        removeConnectionEventListener in interface javax.sql.PooledConnection
      • removeStatementEventListener

        public void removeStatementEventListener​(javax.sql.StatementEventListener listener)
        Specified by:
        removeStatementEventListener in interface javax.sql.PooledConnection
      • finalize

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

        void notifyListeners()
        sends a connectionClosed event.
      • prepareStatement

        java.sql.PreparedStatement prepareStatement​(java.lang.String sql)
                                             throws java.sql.SQLException
        Create or obtain a PreparedStatement from my pool.
        Parameters:
        sql - the SQL statement
        Returns:
        a PoolablePreparedStatement
        Throws:
        java.sql.SQLException
      • prepareStatement

        java.sql.PreparedStatement prepareStatement​(java.lang.String sql,
                                                    int resultSetType,
                                                    int resultSetConcurrency)
                                             throws java.sql.SQLException
        Create or obtain 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:
        java.sql.SQLException
        See Also:
        Connection.prepareStatement(String, int, int)
      • prepareStatement

        java.sql.PreparedStatement prepareStatement​(java.lang.String sql,
                                                    int autoGeneratedKeys)
                                             throws java.sql.SQLException
        Create or obtain 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:
        java.sql.SQLException
        See Also:
        Connection.prepareStatement(String, int)
      • prepareStatement

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

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

        java.sql.PreparedStatement prepareStatement​(java.lang.String sql,
                                                    java.lang.String[] columnNames)
                                             throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql,
                                             int autoGeneratedKeys)
        Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql,
                                             int resultSetType,
                                             int resultSetConcurrency,
                                             int resultSetHoldability)
        Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql,
                                             int[] columnIndexes)
        Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql,
                                             java.lang.String[] columnNames)
        Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql,
                                             int resultSetType,
                                             int resultSetConcurrency)
        Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql)
        Create a {*link PooledConnectionImpl.PStmtKey} for the given arguments.
      • normalizeSQL

        protected java.lang.String normalizeSQL​(java.lang.String sql)
        Normalize the given SQL statement, producing a cannonical form that is semantically equivalent to the original.
      • makeObject

        public java.lang.Object makeObject​(java.lang.Object obj)
                                    throws java.lang.Exception
        My {*link KeyedPoolableObjectFactory} method for creating {*link PreparedStatement}s.
        Specified by:
        makeObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        obj - the key for the {*link PreparedStatement} to be created
        Throws:
        java.lang.Exception
      • destroyObject

        public void destroyObject​(java.lang.Object key,
                                  java.lang.Object obj)
                           throws java.lang.Exception
        My {*link KeyedPoolableObjectFactory} method for destroying {*link PreparedStatement}s.
        Specified by:
        destroyObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - ignored
        obj - the {*link PreparedStatement} to be destroyed.
        Throws:
        java.lang.Exception
      • validateObject

        public boolean validateObject​(java.lang.Object key,
                                      java.lang.Object obj)
        My {*link KeyedPoolableObjectFactory} method for validating {*link PreparedStatement}s.
        Specified by:
        validateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - ignored
        obj - ignored
        Returns:
        true
      • activateObject

        public void activateObject​(java.lang.Object key,
                                   java.lang.Object obj)
                            throws java.lang.Exception
        My {*link KeyedPoolableObjectFactory} method for activating {*link PreparedStatement}s.
        Specified by:
        activateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - ignored
        obj - ignored
        Throws:
        java.lang.Exception
      • passivateObject

        public void passivateObject​(java.lang.Object key,
                                    java.lang.Object obj)
                             throws java.lang.Exception
        My {*link KeyedPoolableObjectFactory} method for passivating {*link PreparedStatement}s. Currently invokes {*link PreparedStatement#clearParameters}.
        Specified by:
        passivateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - ignored
        obj - a {*link PreparedStatement}
        Throws:
        java.lang.Exception
      • isAccessToUnderlyingConnectionAllowed

        public boolean isAccessToUnderlyingConnectionAllowed()
        Returns the value of the accessToUnderlyingConnectionAllowed property.
        Returns:
        true if access to the underlying is allowed, false otherwise.
      • 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.