Package 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.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
ADelegatingConnection
that poolsPreparedStatement
s.The
prepareStatement(java.lang.String)
andprepareCall(java.lang.String)
methods, rather than creating a new PreparedStatement each time, may actually pull the statement from a pool of unused statements. TheStatement.close()
method of the returned statement doesn't actually close the statement, but rather returns it to the pool. (SeePoolablePreparedStatement
,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 Summary
Fields Modifier and Type Field Description protected org.apache.commons.pool.KeyedObjectPool
_pstmtPool
Pool ofPreparedStatement
s.-
Fields inherited from class org.apache.commons.dbcp.DelegatingConnection
_closed, _conn
-
-
Constructor Summary
Constructors Constructor Description PoolingConnection(java.sql.Connection c)
Constructor.PoolingConnection(java.sql.Connection c, org.apache.commons.pool.KeyedObjectPool pool)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort(java.util.concurrent.Executor executor)
void
activateObject(java.lang.Object key, java.lang.Object obj)
KeyedPoolableObjectFactory
method for activating pooled statements.void
close()
Close and free allPreparedStatement
s orCallableStatement
from the pool, and close the underlying connection.protected java.lang.Object
createKey(java.lang.String sql)
Create a PStmtKey for the given arguments.protected java.lang.Object
createKey(java.lang.String sql, byte stmtType)
Create a PStmtKey for the given arguments.protected java.lang.Object
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Create a PStmtKey for the given arguments.protected java.lang.Object
createKey(java.lang.String sql, int resultSetType, int resultSetConcurrency, byte stmtType)
Create a PStmtKey for the given arguments.void
destroyObject(java.lang.Object key, java.lang.Object obj)
KeyedPoolableObjectFactory
method for destroying PoolablePreparedStatements and PoolableCallableStatements.int
getNetworkTimeout()
java.lang.String
getSchema()
java.lang.Object
makeObject(java.lang.Object obj)
KeyedPoolableObjectFactory
method for creatingPoolablePreparedStatement
s orPoolableCallableStatement
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.void
passivateObject(java.lang.Object key, java.lang.Object obj)
KeyedPoolableObjectFactory
method for passivatingPreparedStatement
s orCallableStatement
s.java.sql.CallableStatement
prepareCall(java.lang.String sql)
Create or obtain aCallableStatement
from the pool.java.sql.CallableStatement
prepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Create or obtain aCallableStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql)
Create or obtain aPreparedStatement
from the pool.java.sql.PreparedStatement
prepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)
Create or obtain aPreparedStatement
from the pool.void
setNetworkTimeout(java.util.concurrent.Executor executor, int milliseconds)
void
setSchema(java.lang.String schema)
java.lang.String
toString()
Returns a string representation of the metadata associated with the innnermost delegate connection.boolean
validateObject(java.lang.Object key, java.lang.Object obj)
KeyedPoolableObjectFactory
method for validating pooled statements.-
Methods inherited from class org.apache.commons.dbcp.DelegatingConnection
activate, checkOpen, clearWarnings, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, equals, getAutoCommit, getCatalog, getClientInfo, getClientInfo, getDelegate, getDelegateInternal, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, handleException, hashCode, innermostDelegateEquals, isClosed, isReadOnly, isValid, isWrapperFor, nativeSQL, passivate, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setClientInfo, setClientInfo, setDelegate, setHoldability, setReadOnly, setSavepoint, setSavepoint, setTransactionIsolation, setTypeMap, unwrap
-
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
beginRequest, clearWarnings, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, endRequest, getAutoCommit, getCatalog, getClientInfo, getClientInfo, getHoldability, getMetaData, getTransactionIsolation, getTypeMap, getWarnings, isClosed, isReadOnly, isValid, nativeSQL, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setClientInfo, setClientInfo, setHoldability, setReadOnly, setSavepoint, setSavepoint, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid, setTransactionIsolation, setTypeMap
-
-
-
-
Constructor Detail
-
PoolingConnection
public PoolingConnection(java.sql.Connection c)
Constructor.- Parameters:
c
- the underlyingConnection
.
-
PoolingConnection
public PoolingConnection(java.sql.Connection c, org.apache.commons.pool.KeyedObjectPool pool)
Constructor.- Parameters:
c
- the underlyingConnection
.pool
-KeyedObjectPool
ofPreparedStatement
s andCallableStatement
s.
-
-
Method Detail
-
close
public void close() throws java.sql.SQLException
Close and free allPreparedStatement
s orCallableStatement
from the pool, and close the underlying connection.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.sql.Connection
- Overrides:
close
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
prepareStatement
public java.sql.PreparedStatement prepareStatement(java.lang.String sql) throws java.sql.SQLException
Create or obtain aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfacejava.sql.Connection
- Overrides:
prepareStatement
in classDelegatingConnection
- 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 aPreparedStatement
from the pool.- Specified by:
prepareStatement
in interfacejava.sql.Connection
- Overrides:
prepareStatement
in classDelegatingConnection
- Parameters:
sql
- the sql string used to define the PreparedStatementresultSetType
- result set typeresultSetConcurrency
- 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 aCallableStatement
from the pool.- Specified by:
prepareCall
in interfacejava.sql.Connection
- Overrides:
prepareCall
in classDelegatingConnection
- 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 aCallableStatement
from the pool.- Specified by:
prepareCall
in interfacejava.sql.Connection
- Overrides:
prepareCall
in classDelegatingConnection
- Parameters:
sql
- the sql string used to define the CallableStatementresultSetType
- result set typeresultSetConcurrency
- 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 statementresultSetType
- result set typeresultSetConcurrency
- 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 statementresultSetType
- result set typeresultSetConcurrency
- result set concurrencystmtType
- statement type - eitherSTATEMENT_CALLABLESTMT
orSTATEMENT_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 statementstmtType
- statement type - eitherSTATEMENT_CALLABLESTMT
orSTATEMENT_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 creatingPoolablePreparedStatement
s orPoolableCallableStatement
s. ThestmtType
field in the key determines whether a PoolablePreparedStatement or PoolableCallableStatement is created.- Specified by:
makeObject
in interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory
- Parameters:
obj
- the key for thePreparedStatement
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 interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory
- Parameters:
key
- ignoredobj
- 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 interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory
- Parameters:
key
- ignoredobj
- 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 interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory
- Parameters:
key
- ignoredobj
- 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 passivatingPreparedStatement
s orCallableStatement
s. InvokesPreparedStatement.clearParameters()
.- Specified by:
passivateObject
in interfaceorg.apache.commons.pool.KeyedPoolableObjectFactory
- Parameters:
key
- ignoredobj
- aPreparedStatement
- 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 classDelegatingConnection
-
getNetworkTimeout
public int getNetworkTimeout() throws java.sql.SQLException
- Specified by:
getNetworkTimeout
in interfacejava.sql.Connection
- Overrides:
getNetworkTimeout
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
setNetworkTimeout
public void setNetworkTimeout(java.util.concurrent.Executor executor, int milliseconds) throws java.sql.SQLException
- Specified by:
setNetworkTimeout
in interfacejava.sql.Connection
- Overrides:
setNetworkTimeout
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
abort
public void abort(java.util.concurrent.Executor executor) throws java.sql.SQLException
- Specified by:
abort
in interfacejava.sql.Connection
- Overrides:
abort
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
getSchema
public java.lang.String getSchema() throws java.sql.SQLException
- Specified by:
getSchema
in interfacejava.sql.Connection
- Overrides:
getSchema
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
setSchema
public void setSchema(java.lang.String schema) throws java.sql.SQLException
- Specified by:
setSchema
in interfacejava.sql.Connection
- Overrides:
setSchema
in classDelegatingConnection
- Throws:
java.sql.SQLException
-
-