Class PoolingConnection

  • All Implemented Interfaces:
    java.lang.AutoCloseable, java.sql.Connection, java.sql.Wrapper, org.apache.commons.pool.KeyedPoolableObjectFactory

    public class PoolingConnection
    extends DelegatingConnection
    implements java.sql.Connection, org.apache.commons.pool.KeyedPoolableObjectFactory
    A DelegatingConnection that pools PreparedStatements.

    The prepareStatement(java.lang.String) and prepareCall(java.lang.String) methods, rather than creating a new PreparedStatement each time, may actually pull the statement from a pool of unused statements. The Statement.close() method of the returned statement doesn't actually close the statement, but rather returns it to the pool. (See PoolablePreparedStatement, PoolableCallableStatement.)

    Version:
    $Revision: 885261 $ $Date: 2009-11-29 15:07:02 -0500 (Sun, 29 Nov 2009) $
    Author:
    Rodney Waldhoff, Dirk Verbeeck
    See Also:
    PoolablePreparedStatement
    • Field Detail

      • _pstmtPool

        protected org.apache.commons.pool.KeyedObjectPool _pstmtPool
        Pool of PreparedStatements. and CallableStatements
    • Constructor Detail

      • PoolingConnection

        public PoolingConnection​(java.sql.Connection c)
        Constructor.
        Parameters:
        c - the underlying Connection.
      • PoolingConnection

        public PoolingConnection​(java.sql.Connection c,
                                 org.apache.commons.pool.KeyedObjectPool pool)
        Constructor.
        Parameters:
        c - the underlying Connection.
        pool - KeyedObjectPool of PreparedStatements and CallableStatements.
    • Method Detail

      • close

        public void close()
                   throws java.sql.SQLException
        Close and free all PreparedStatements or CallableStatement from the pool, and close the underlying connection.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.sql.Connection
        Overrides:
        close in class DelegatingConnection
        Throws:
        java.sql.SQLException
      • prepareStatement

        public java.sql.PreparedStatement prepareStatement​(java.lang.String sql)
                                                    throws java.sql.SQLException
        Create or obtain a PreparedStatement from the pool.
        Specified by:
        prepareStatement in interface java.sql.Connection
        Overrides:
        prepareStatement in class DelegatingConnection
        Parameters:
        sql - the sql string used to define the PreparedStatement
        Returns:
        a PoolablePreparedStatement
        Throws:
        java.sql.SQLException
      • prepareStatement

        public java.sql.PreparedStatement prepareStatement​(java.lang.String sql,
                                                           int resultSetType,
                                                           int resultSetConcurrency)
                                                    throws java.sql.SQLException
        Create or obtain a PreparedStatement from the pool.
        Specified by:
        prepareStatement in interface java.sql.Connection
        Overrides:
        prepareStatement in class DelegatingConnection
        Parameters:
        sql - the sql string used to define the PreparedStatement
        resultSetType - result set type
        resultSetConcurrency - result set concurrency
        Returns:
        a PoolablePreparedStatement
        Throws:
        java.sql.SQLException
      • prepareCall

        public java.sql.CallableStatement prepareCall​(java.lang.String sql)
                                               throws java.sql.SQLException
        Create or obtain a CallableStatement from the pool.
        Specified by:
        prepareCall in interface java.sql.Connection
        Overrides:
        prepareCall in class DelegatingConnection
        Parameters:
        sql - the sql string used to define the CallableStatement
        Returns:
        a PoolableCallableStatement
        Throws:
        java.sql.SQLException
        Since:
        1.3
      • prepareCall

        public java.sql.CallableStatement prepareCall​(java.lang.String sql,
                                                      int resultSetType,
                                                      int resultSetConcurrency)
                                               throws java.sql.SQLException
        Create or obtain a CallableStatement from the pool.
        Specified by:
        prepareCall in interface java.sql.Connection
        Overrides:
        prepareCall in class DelegatingConnection
        Parameters:
        sql - the sql string used to define the CallableStatement
        resultSetType - result set type
        resultSetConcurrency - result set concurrency
        Returns:
        a PoolableCallableStatement
        Throws:
        java.sql.SQLException
        Since:
        1.3
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql,
                                             int resultSetType,
                                             int resultSetConcurrency)
        Create a PStmtKey for the given arguments.
        Parameters:
        sql - the sql string used to define the statement
        resultSetType - result set type
        resultSetConcurrency - result set concurrency
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql,
                                             int resultSetType,
                                             int resultSetConcurrency,
                                             byte stmtType)
        Create a PStmtKey for the given arguments.
        Parameters:
        sql - the sql string used to define the statement
        resultSetType - result set type
        resultSetConcurrency - result set concurrency
        stmtType - statement type - either STATEMENT_CALLABLESTMT or STATEMENT_PREPAREDSTMT
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql)
        Create a PStmtKey for the given arguments.
        Parameters:
        sql - the sql string used to define the statement
      • createKey

        protected java.lang.Object createKey​(java.lang.String sql,
                                             byte stmtType)
        Create a PStmtKey for the given arguments.
        Parameters:
        sql - the sql string used to define the statement
        stmtType - statement type - either STATEMENT_CALLABLESTMT or STATEMENT_PREPAREDSTMT
      • 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
        KeyedPoolableObjectFactory method for creating PoolablePreparedStatements or PoolableCallableStatements. The stmtType field in the key determines whether a PoolablePreparedStatement or PoolableCallableStatement is created.
        Specified by:
        makeObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        obj - the key for the PreparedStatement to be created
        Throws:
        java.lang.Exception
        See Also:
        createKey(String, int, int, byte)
      • destroyObject

        public void destroyObject​(java.lang.Object key,
                                  java.lang.Object obj)
                           throws java.lang.Exception
        KeyedPoolableObjectFactory method for destroying PoolablePreparedStatements and PoolableCallableStatements. Closes the underlying statement.
        Specified by:
        destroyObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - ignored
        obj - the pooled statement to be destroyed.
        Throws:
        java.lang.Exception
      • validateObject

        public boolean validateObject​(java.lang.Object key,
                                      java.lang.Object obj)
        KeyedPoolableObjectFactory method for validating pooled statements. Currently always returns true.
        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
        KeyedPoolableObjectFactory method for activating pooled statements.
        Specified by:
        activateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - ignored
        obj - pooled statement to be activated
        Throws:
        java.lang.Exception
      • passivateObject

        public void passivateObject​(java.lang.Object key,
                                    java.lang.Object obj)
                             throws java.lang.Exception
        KeyedPoolableObjectFactory method for passivating PreparedStatements or CallableStatements. Invokes PreparedStatement.clearParameters().
        Specified by:
        passivateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
        Parameters:
        key - ignored
        obj - a PreparedStatement
        Throws:
        java.lang.Exception
      • toString

        public java.lang.String toString()
        Description copied from class: DelegatingConnection
        Returns a string representation of the metadata associated with the innnermost delegate connection.
        Overrides:
        toString in class DelegatingConnection
      • getNetworkTimeout

        public int getNetworkTimeout()
                              throws java.sql.SQLException
        Specified by:
        getNetworkTimeout in interface java.sql.Connection
        Overrides:
        getNetworkTimeout in class DelegatingConnection
        Throws:
        java.sql.SQLException
      • setNetworkTimeout

        public void setNetworkTimeout​(java.util.concurrent.Executor executor,
                                      int milliseconds)
                               throws java.sql.SQLException
        Specified by:
        setNetworkTimeout in interface java.sql.Connection
        Overrides:
        setNetworkTimeout in class DelegatingConnection
        Throws:
        java.sql.SQLException
      • abort

        public void abort​(java.util.concurrent.Executor executor)
                   throws java.sql.SQLException
        Specified by:
        abort in interface java.sql.Connection
        Overrides:
        abort in class DelegatingConnection
        Throws:
        java.sql.SQLException
      • getSchema

        public java.lang.String getSchema()
                                   throws java.sql.SQLException
        Specified by:
        getSchema in interface java.sql.Connection
        Overrides:
        getSchema in class DelegatingConnection
        Throws:
        java.sql.SQLException
      • setSchema

        public void setSchema​(java.lang.String schema)
                       throws java.sql.SQLException
        Specified by:
        setSchema in interface java.sql.Connection
        Overrides:
        setSchema in class DelegatingConnection
        Throws:
        java.sql.SQLException