org.apache.commons.dbcp
Class PoolingConnection

java.lang.Object
  extended byorg.apache.commons.dbcp.AbandonedTrace
      extended byorg.apache.commons.dbcp.DelegatingConnection
          extended byorg.apache.commons.dbcp.PoolingConnection
All Implemented Interfaces:
Connection, KeyedPoolableObjectFactory

public class PoolingConnection
extends DelegatingConnection
implements Connection, 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.)

Version:
$Revision: 892307 $ $Date: 2014-01-01 00:27:28 +0100 (Wed, 01 Jan 2014) $
Author:
Rodney Waldhoff, Dirk Verbeeck
See Also:
PoolablePreparedStatement

Field Summary
protected  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(Connection c)
          Constructor.
PoolingConnection(Connection c, KeyedObjectPool pool)
          Constructor.
 
Method Summary
 void activateObject(Object key, Object obj)
          My KeyedPoolableObjectFactory method for activating PreparedStatements.
 void close()
          Close and free all PreparedStatements from my pool, and close my underlying connection.
protected  Object createKey(String sql)
          Create a PStmtKey for the given arguments.
protected  Object createKey(String sql, int resultSetType, int resultSetConcurrency)
          Create a PStmtKey for the given arguments.
 void destroyObject(Object key, Object obj)
          My KeyedPoolableObjectFactory method for destroying PreparedStatements.
 Object makeObject(Object obj)
          My KeyedPoolableObjectFactory method for creating PreparedStatements.
protected  String normalizeSQL(String sql)
          Normalize the given SQL statement, producing a cannonical form that is semantically equivalent to the original.
 void passivateObject(Object key, Object obj)
          My KeyedPoolableObjectFactory method for passivating PreparedStatements.
 PreparedStatement prepareStatement(String sql)
          Create or obtain a PreparedStatement from my pool.
 PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
          Create or obtain a PreparedStatement from my pool.
 String toString()
          Returns a string representation of the metadata associated with the innnermost delegate connection.
 boolean validateObject(Object key, Object obj)
          My KeyedPoolableObjectFactory method for validating PreparedStatements.
 
Methods inherited from class org.apache.commons.dbcp.DelegatingConnection
activate, checkOpen, clearWarnings, commit, createStatement, createStatement, createStatement, equals, getAutoCommit, getCatalog, getDelegate, getHoldability, getInnermostDelegate, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, handleException, hashCode, innermostDelegateEquals, isClosed, isReadOnly, nativeSQL, passivate, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setDelegate, setHoldability, setReadOnly, setSavepoint, setSavepoint, 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, finalize, getClass, 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 KeyedObjectPool _pstmtPool
My pool of PreparedStatements.

Constructor Detail

PoolingConnection

public PoolingConnection(Connection c)
Constructor.

Parameters:
c - the underlying Connection.

PoolingConnection

public PoolingConnection(Connection c,
                         KeyedObjectPool pool)
Constructor.

Parameters:
c - the underlying Connection.
pool - KeyedObjectPool of PreparedStatements
Method Detail

close

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

Specified by:
close in interface Connection
Overrides:
close in class DelegatingConnection
Throws:
SQLException

prepareStatement

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

Specified by:
prepareStatement in interface Connection
Overrides:
prepareStatement in class DelegatingConnection
Returns:
a PoolablePreparedStatement
Throws:
SQLException

prepareStatement

public PreparedStatement prepareStatement(String sql,
                                          int resultSetType,
                                          int resultSetConcurrency)
                                   throws SQLException
Create or obtain a PreparedStatement from my pool.

Specified by:
prepareStatement in interface Connection
Overrides:
prepareStatement in class DelegatingConnection
Returns:
a PoolablePreparedStatement
Throws:
SQLException

createKey

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


createKey

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


normalizeSQL

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


makeObject

public Object makeObject(Object obj)
                  throws Exception
My KeyedPoolableObjectFactory method for creating PreparedStatements.

Specified by:
makeObject in interface KeyedPoolableObjectFactory
Parameters:
obj - the key for the PreparedStatement to be created
Throws:
Exception

destroyObject

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

Specified by:
destroyObject in interface KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - the PreparedStatement to be destroyed.
Throws:
Exception

validateObject

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

Specified by:
validateObject in interface KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - ignored
Returns:
true

activateObject

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

Specified by:
activateObject in interface KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - ignored
Throws:
Exception

passivateObject

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

Specified by:
passivateObject in interface KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - a PreparedStatement
Throws:
Exception

toString

public 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


Copyright © The Apache Software Foundation. All Rights Reserved.