org.apache.commons.dbcp
Class PoolingConnection

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

public class PoolingConnection
extends DelegatingConnection
implements Connection, 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: 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
          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)
          KeyedPoolableObjectFactory method for activating pooled statements.
 void close()
          Close and free all PreparedStatements or CallableStatement from the pool, and close the underlying connection.
protected  Object createKey(String sql)
          Create a PStmtKey for the given arguments.
protected  Object createKey(String sql, byte stmtType)
          Create a PStmtKey for the given arguments.
protected  Object createKey(String sql, int resultSetType, int resultSetConcurrency)
          Create a PStmtKey for the given arguments.
protected  Object createKey(String sql, int resultSetType, int resultSetConcurrency, byte stmtType)
          Create a PStmtKey for the given arguments.
 void destroyObject(Object key, Object obj)
          KeyedPoolableObjectFactory method for destroying PoolablePreparedStatements and PoolableCallableStatements.
 Object makeObject(Object obj)
          KeyedPoolableObjectFactory method for creating PoolablePreparedStatements or PoolableCallableStatements.
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)
          KeyedPoolableObjectFactory method for passivating PreparedStatements or CallableStatements.
 CallableStatement prepareCall(String sql)
          Create or obtain a CallableStatement from the pool.
 CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
          Create or obtain a CallableStatement from the pool.
 PreparedStatement prepareStatement(String sql)
          Create or obtain a PreparedStatement from the pool.
 PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
          Create or obtain a PreparedStatement from the pool.
 String toString()
          Returns a string representation of the metadata associated with the innnermost delegate connection.
 boolean validateObject(Object key, Object obj)
          KeyedPoolableObjectFactory method for validating pooled statements.
 
Methods inherited from class org.apache.commons.dbcp.DelegatingConnection
activate, checkOpen, clearWarnings, commit, createStatement, createStatement, createStatement, equals, getAutoCommit, getCatalog, getDelegate, getDelegateInternal, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, handleException, hashCode, innermostDelegateEquals, isClosed, isReadOnly, nativeSQL, passivate, 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, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setHoldability, setReadOnly, setSavepoint, setSavepoint, setTransactionIsolation, setTypeMap
 

Field Detail

_pstmtPool

protected KeyedObjectPool _pstmtPool
Pool of PreparedStatements. and CallableStatements

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

close

public void close()
           throws SQLException
Close and free all PreparedStatements or CallableStatement from the pool, and close the 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 the pool.

Specified by:
prepareStatement in interface Connection
Overrides:
prepareStatement in class DelegatingConnection
Parameters:
sql - the sql string used to define the PreparedStatement
Returns:
a PoolablePreparedStatement
Throws:
SQLException

prepareStatement

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

Specified by:
prepareStatement in interface 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:
SQLException

prepareCall

public CallableStatement prepareCall(String sql)
                              throws SQLException
Create or obtain a CallableStatement from the pool.

Specified by:
prepareCall in interface Connection
Overrides:
prepareCall in class DelegatingConnection
Parameters:
sql - the sql string used to define the CallableStatement
Returns:
a PoolableCallableStatement
Throws:
SQLException
Since:
1.3

prepareCall

public CallableStatement prepareCall(String sql,
                                     int resultSetType,
                                     int resultSetConcurrency)
                              throws SQLException
Create or obtain a CallableStatement from the pool.

Specified by:
prepareCall in interface 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:
SQLException
Since:
1.3

createKey

protected Object createKey(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 Object createKey(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 Object createKey(String sql)
Create a PStmtKey for the given arguments.

Parameters:
sql - the sql string used to define the statement

createKey

protected Object createKey(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 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
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 KeyedPoolableObjectFactory
Parameters:
obj - the key for the PreparedStatement to be created
Throws:
Exception
See Also:
createKey(String, int, int, byte)

destroyObject

public void destroyObject(Object key,
                          Object obj)
                   throws Exception
KeyedPoolableObjectFactory method for destroying PoolablePreparedStatements and PoolableCallableStatements. Closes the underlying statement.

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

validateObject

public boolean validateObject(Object key,
                              Object obj)
KeyedPoolableObjectFactory method for validating pooled statements. Currently always returns true.

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

activateObject

public void activateObject(Object key,
                           Object obj)
                    throws Exception
KeyedPoolableObjectFactory method for activating pooled statements.

Specified by:
activateObject in interface KeyedPoolableObjectFactory
Parameters:
key - ignored
obj - pooled statement to be activated
Throws:
Exception

passivateObject

public void passivateObject(Object key,
                            Object obj)
                     throws Exception
KeyedPoolableObjectFactory method for passivating PreparedStatements or CallableStatements. 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 © 2001-2010 The Apache Software Foundation. All Rights Reserved.