org.apache.commons.dbcp
Class PoolingConnection

java.lang.Object
  |
  +--org.apache.commons.dbcp.AbandonedTrace
        |
        +--org.apache.commons.dbcp.DelegatingConnection
              |
              +--org.apache.commons.dbcp.PoolingConnection
All Implemented Interfaces:
java.sql.Connection, org.apache.commons.pool.KeyedPoolableObjectFactory

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

A DelegatingConnection that pools PreparedStatements.

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

Author:
Rodney Waldhoff (rwaldhof@us.britannica.com)
See Also:
PoolablePreparedStatement

Field Summary
protected  org.apache.commons.pool.KeyedObjectPool _pstmtPool
          My pool of PreparedStatements.
 
Fields inherited from class org.apache.commons.dbcp.DelegatingConnection
_closed, _conn
 
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
 
Constructor Summary
PoolingConnection(java.sql.Connection c)
          Constructor.
PoolingConnection(java.sql.Connection c, org.apache.commons.pool.KeyedObjectPool pool)
          Constructor.
 
Method Summary
 void activateObject(java.lang.Object key, java.lang.Object obj)
          My KeyedPoolableObjectFactory method for activating PreparedStatements.
 void close()
          Close and free all PreparedStatements from my pool, and close my underlying connection.
protected  java.lang.Object createKey(java.lang.String sql)
          Create a PoolingConnection.PStmtKey for the given arguments.
protected  java.lang.Object createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)
          Create a PoolingConnection.PStmtKey for the given arguments.
 void destroyObject(java.lang.Object key, java.lang.Object obj)
          My KeyedPoolableObjectFactory method for destroying PreparedStatements.
 java.lang.Object makeObject(java.lang.Object obj)
          My KeyedPoolableObjectFactory method for creating PreparedStatements.
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.
 void passivateObject(java.lang.Object key, java.lang.Object obj)
          My KeyedPoolableObjectFactory method for passivating PreparedStatements.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
          Create or obtain a PreparedStatement from my pool.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)
          Create or obtain a PreparedStatement from my pool.
 java.lang.String toString()
           
 boolean validateObject(java.lang.Object key, java.lang.Object obj)
          My KeyedPoolableObjectFactory method for validating PreparedStatements.
 
Methods inherited from class org.apache.commons.dbcp.DelegatingConnection
activate, checkOpen, clearWarnings, commit, createStatement, createStatement, getAutoCommit, getCatalog, getDelegate, getInnermostDelegate, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, isClosed, isReadOnly, nativeSQL, passivate, prepareCall, prepareCall, rollback, setAutoCommit, setCatalog, setDelegate, setReadOnly, setTransactionIsolation, setTypeMap
 
Methods inherited from class org.apache.commons.dbcp.AbandonedTrace
addTrace, clearTrace, getConfig, getLastUsed, getTrace, printStackTrace, removeTrace, setLastUsed, setLastUsed, setStackTrace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.sql.Connection
clearWarnings, commit, createStatement, createStatement, createStatement, getAutoCommit, getCatalog, getHoldability, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, isClosed, isReadOnly, nativeSQL, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setHoldability, setReadOnly, setSavepoint, setSavepoint, setTransactionIsolation, setTypeMap
 

Field Detail

_pstmtPool

protected org.apache.commons.pool.KeyedObjectPool _pstmtPool
My pool of PreparedStatements.

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.
Method Detail

activateObject

public void activateObject(java.lang.Object key,
                           java.lang.Object obj)
My KeyedPoolableObjectFactory method for activating PreparedStatements. (Currently a no-op.)

Specified by:
activateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - ignored

close

public void close()
           throws java.sql.SQLException
Close and free all PreparedStatements from my pool, and close my underlying connection.

Specified by:
close in interface java.sql.Connection
Overrides:
close in class DelegatingConnection
java.sql.SQLException

createKey

protected java.lang.Object createKey(java.lang.String sql)
Create a PoolingConnection.PStmtKey for the given arguments.


createKey

protected java.lang.Object createKey(java.lang.String sql,
                                     int resultSetType,
                                     int resultSetConcurrency)
Create a PoolingConnection.PStmtKey for the given arguments.


destroyObject

public void destroyObject(java.lang.Object key,
                          java.lang.Object obj)
My KeyedPoolableObjectFactory method for destroying PreparedStatements.

Specified by:
destroyObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - the PreparedStatement to be destroyed.

makeObject

public java.lang.Object makeObject(java.lang.Object obj)
My KeyedPoolableObjectFactory method for creating PreparedStatements.

Specified by:
makeObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
Parameters:
obj - the key for the PreparedStatement to be created

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.


passivateObject

public void passivateObject(java.lang.Object key,
                            java.lang.Object obj)
My KeyedPoolableObjectFactory method for passivating PreparedStatements. Currently invokes PreparedStatement.clearParameters().

Specified by:
passivateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - a PreparedStatement

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql)
                                            throws java.sql.SQLException
Create or obtain a PreparedStatement from my pool.

Specified by:
prepareStatement in interface java.sql.Connection
Overrides:
prepareStatement in class DelegatingConnection
Returns:
a PoolablePreparedStatement
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 my pool.

Specified by:
prepareStatement in interface java.sql.Connection
Overrides:
prepareStatement in class DelegatingConnection
Returns:
a PoolablePreparedStatement
java.sql.SQLException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

validateObject

public boolean validateObject(java.lang.Object key,
                              java.lang.Object obj)
My KeyedPoolableObjectFactory method for validating PreparedStatements.

Specified by:
validateObject in interface org.apache.commons.pool.KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - ignored
Returns:
true


Copyright © 2001 Apache Software Foundation. Documenation generated August 12 2002.