public class PoolingConnection extends DelegatingConnection<Connection> implements KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
DelegatingConnection
that pools PreparedStatement
s.
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
.)
PoolablePreparedStatement
Modifier and Type | Class and Description |
---|---|
protected static class |
PoolingConnection.StatementType
The possible statement types.
|
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
Constructor and Description |
---|
PoolingConnection(Connection c)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
activateObject(PStmtKey key,
PooledObject<DelegatingPreparedStatement> p)
KeyedPooledObjectFactory method for activating
pooled statements. |
void |
close()
Close and free all
PreparedStatement s or
CallableStatement s from the pool, and close the underlying
connection. |
protected PStmtKey |
createKey(String sql)
Create a PStmtKey for the given arguments.
|
protected PStmtKey |
createKey(String sql,
int autoGeneratedKeys) |
protected PStmtKey |
createKey(String sql,
int resultSetType,
int resultSetConcurrency)
Create a PStmtKey for the given arguments.
|
protected PStmtKey |
createKey(String sql,
int resultSetType,
int resultSetConcurrency,
PoolingConnection.StatementType stmtType)
Create a PStmtKey for the given arguments.
|
protected PStmtKey |
createKey(String sql,
PoolingConnection.StatementType stmtType)
Create a PStmtKey for the given arguments.
|
void |
destroyObject(PStmtKey key,
PooledObject<DelegatingPreparedStatement> p)
KeyedPooledObjectFactory method for destroying
PoolablePreparedStatements and PoolableCallableStatements. |
PooledObject<DelegatingPreparedStatement> |
makeObject(PStmtKey key)
KeyedPooledObjectFactory method for creating
PoolablePreparedStatement s or PoolableCallableStatement s. |
protected String |
normalizeSQL(String sql)
Normalize the given SQL statement, producing a
canonical form that is semantically equivalent to the original.
|
void |
passivateObject(PStmtKey key,
PooledObject<DelegatingPreparedStatement> p)
|
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 autoGeneratedKeys) |
PreparedStatement |
prepareStatement(String sql,
int resultSetType,
int resultSetConcurrency)
Create or obtain a
PreparedStatement from the pool. |
void |
setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool) |
String |
toString()
Returns a string representation of the metadata associated with
the innermost delegate connection.
|
boolean |
validateObject(PStmtKey key,
PooledObject<DelegatingPreparedStatement> p)
KeyedPooledObjectFactory method for validating
pooled statements. |
abort, activate, checkOpen, clearCachedState, clearWarnings, closeInternal, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCacheState, getCatalog, getClientInfo, getClientInfo, getDefaultQueryTimeout, getDelegate, getDelegateInternal, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, handleException, innermostDelegateEquals, isClosed, isClosedInternal, isReadOnly, isValid, isWrapperFor, nativeSQL, passivate, prepareCall, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCacheState, setCatalog, setClientInfo, setClientInfo, setClosedInternal, setDefaultQueryTimeout, setDelegate, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, unwrap
addTrace, clearTrace, getLastUsed, getTrace, removeTrace, setLastUsed, setLastUsed
public PoolingConnection(Connection c)
c
- the underlying Connection
.public void setStatementPool(KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool)
public void close() throws SQLException
PreparedStatement
s or
CallableStatement
s from the pool, and close the underlying
connection.close
in interface AutoCloseable
close
in interface Connection
close
in class DelegatingConnection<Connection>
SQLException
- Error closing connectionpublic PreparedStatement prepareStatement(String sql) throws SQLException
PreparedStatement
from the pool.prepareStatement
in interface Connection
prepareStatement
in class DelegatingConnection<Connection>
sql
- the sql string used to define the PreparedStatementPoolablePreparedStatement
SQLException
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
prepareStatement
in interface Connection
prepareStatement
in class DelegatingConnection<Connection>
SQLException
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
PreparedStatement
from the pool.prepareStatement
in interface Connection
prepareStatement
in class DelegatingConnection<Connection>
sql
- the sql string used to define the PreparedStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyPoolablePreparedStatement
SQLException
- An error occurredpublic CallableStatement prepareCall(String sql) throws SQLException
CallableStatement
from the pool.prepareCall
in interface Connection
prepareCall
in class DelegatingConnection<Connection>
sql
- the sql string used to define the CallableStatementPoolableCallableStatement
SQLException
- An error occurredpublic CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
CallableStatement
from the pool.prepareCall
in interface Connection
prepareCall
in class DelegatingConnection<Connection>
sql
- the sql string used to define the CallableStatementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyPoolableCallableStatement
SQLException
- An error occurredprotected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency)
sql
- the sql string used to define the statementresultSetType
- result set typeresultSetConcurrency
- result set concurrencyprotected PStmtKey createKey(String sql, int resultSetType, int resultSetConcurrency, PoolingConnection.StatementType stmtType)
sql
- the sql string used to define the statementresultSetType
- result set typeresultSetConcurrency
- result set concurrencystmtType
- statement typeprotected PStmtKey createKey(String sql)
sql
- the sql string used to define the statementprotected PStmtKey createKey(String sql, PoolingConnection.StatementType stmtType)
sql
- the SQL string used to define the statementstmtType
- statement typeprotected String normalizeSQL(String sql)
sql
- The SQL statementpublic PooledObject<DelegatingPreparedStatement> makeObject(PStmtKey key) throws Exception
KeyedPooledObjectFactory
method for creating
PoolablePreparedStatement
s or PoolableCallableStatement
s.
The stmtType
field in the key determines whether
a PoolablePreparedStatement or PoolableCallableStatement is created.makeObject
in interface KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
key
- the key for the PreparedStatement
to be createdException
- An error occurredcreateKey(String, int, int, StatementType)
public void destroyObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> p) throws Exception
KeyedPooledObjectFactory
method for destroying
PoolablePreparedStatements and PoolableCallableStatements.
Closes the underlying statement.destroyObject
in interface KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
key
- ignoredp
- the wrapped pooled statement to be destroyed.Exception
- An error occurredKeyedPooledObjectFactory.validateObject(K, org.apache.tomcat.dbcp.pool2.PooledObject<V>)
,
KeyedObjectPool.invalidateObject(K, V)
public boolean validateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> p)
KeyedPooledObjectFactory
method for validating
pooled statements. Currently always returns true.validateObject
in interface KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
key
- ignoredp
- ignoredtrue
public void activateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> p) throws Exception
KeyedPooledObjectFactory
method for activating
pooled statements.activateObject
in interface KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
key
- ignoredp
- wrapped pooled statement to be activatedException
- An error occurredKeyedPooledObjectFactory.destroyObject(K, org.apache.tomcat.dbcp.pool2.PooledObject<V>)
public void passivateObject(PStmtKey key, PooledObject<DelegatingPreparedStatement> p) throws Exception
KeyedPooledObjectFactory
method for passivating
PreparedStatement
s or CallableStatement
s.
Invokes PreparedStatement.clearParameters()
.passivateObject
in interface KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement>
key
- ignoredp
- a wrapped PreparedStatement
Exception
- An error occurredKeyedPooledObjectFactory.destroyObject(K, org.apache.tomcat.dbcp.pool2.PooledObject<V>)
public String toString()
DelegatingConnection
toString
in class DelegatingConnection<Connection>
Copyright © 2000-2016 Apache Software Foundation. All Rights Reserved.