Package org.apache.commons.dbcp2
Class PoolableConnectionFactory
java.lang.Object
org.apache.commons.dbcp2.PoolableConnectionFactory
- All Implemented Interfaces:
PooledObjectFactory<PoolableConnection>
- Direct Known Subclasses:
PoolableManagedConnectionFactory
public class PoolableConnectionFactory
extends Object
implements PooledObjectFactory<PoolableConnection>
A
PooledObjectFactory
that creates PoolableConnection
s.- Since:
- 2.0
-
Constructor Summary
ConstructorDescriptionPoolableConnectionFactory
(ConnectionFactory connFactory, ObjectName dataSourceJmxObjectName) Creates a newPoolableConnectionFactory
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
void
boolean
Gets the cache state.Gets the connection factory.protected AtomicLong
Deprecated.Gets the default query timeout Duration.Deprecated.int
SQL_STATE codes considered to signal fatal conditions.Gets the Maximum connection duration.long
Gets the Maximum connection lifetime in milliseconds.protected int
getPool()
Returns theObjectPool
in whichConnection
s are pooled.boolean
Gets the query timeout in seconds.int
Deprecated.protected void
boolean
boolean
Deprecated.boolean
True means that validation will fail immediately for connections that have previously thrown SQLExceptions with SQL_STATE indicating fatal disconnection errors.boolean
void
void
setAutoCommitOnReturn
(boolean autoCommitOnReturn) void
setCacheState
(boolean cacheState) void
setClearStatementPoolOnReturn
(boolean clearStatementPoolOnReturn) Sets whether the pool of statements (which was enabled withsetPoolStatements(boolean)
) should be cleared when the connection is returned to its pool.void
setConnectionInitSql
(Collection<String> connectionInitSqls) Sets the SQL statements I use to initialize newly createdConnection
s.void
setDefaultAutoCommit
(Boolean defaultAutoCommit) Sets the default "auto commit" setting for borrowedConnection
svoid
setDefaultCatalog
(String defaultCatalog) Sets the default "catalog" setting for borrowedConnection
svoid
setDefaultQueryTimeout
(Integer defaultQueryTimeoutSeconds) Deprecated.void
setDefaultQueryTimeout
(Duration defaultQueryTimeoutDuration) Sets the query timeout Duration.void
setDefaultReadOnly
(Boolean defaultReadOnly) Sets the default "read only" setting for borrowedConnection
svoid
setDefaultSchema
(String defaultSchema) Sets the default "schema" setting for borrowedConnection
svoid
setDefaultTransactionIsolation
(int defaultTransactionIsolation) Sets the default "Transaction Isolation" setting for borrowedConnection
svoid
setDisconnectionSqlCodes
(Collection<String> disconnectionSqlCodes) void
setEnableAutoCommitOnReturn
(boolean autoCommitOnReturn) Deprecated.void
setFastFailValidation
(boolean fastFailValidation) void
setMaxConn
(Duration maxConnDuration) Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation.void
setMaxConnLifetimeMillis
(long maxConnLifetimeMillis) Deprecated.UsesetMaxConn(Duration)
.void
setMaxOpenPreparedStatements
(int maxOpenPreparedStatements) Sets the maximum number of open prepared statements.void
setMaxOpenPrepatedStatements
(int maxOpenPreparedStatements) Deprecated.void
setPool
(ObjectPool<PoolableConnection> pool) Sets theObjectPool
in which to poolConnection
s.void
setPoolStatements
(boolean poolStatements) void
setRollbackOnReturn
(boolean rollbackOnReturn) void
setValidationQuery
(String validationQuery) Sets the query I use tovalidate
Connection
s.void
setValidationQueryTimeout
(int validationQueryTimeoutSeconds) Deprecated.void
setValidationQueryTimeout
(Duration validationQueryTimeoutDuration) Sets the validation query timeout, the amount of time, that connection validation will wait for a response from the database when executing a validation query.void
Validates the given connection if it is open.boolean
-
Constructor Details
-
PoolableConnectionFactory
Creates a newPoolableConnectionFactory
.- Parameters:
connFactory
- theConnectionFactory
from which to obtain baseConnection
sdataSourceJmxObjectName
- The JMX object name, may be null.
-
-
Method Details
-
activateObject
- Specified by:
activateObject
in interfacePooledObjectFactory<PoolableConnection>
- Throws:
SQLException
-
destroyObject
- Specified by:
destroyObject
in interfacePooledObjectFactory<PoolableConnection>
- Throws:
SQLException
-
destroyObject
- Specified by:
destroyObject
in interfacePooledObjectFactory<PoolableConnection>
- Throws:
SQLException
- Since:
- 2.9.0
-
getCacheState
Gets the cache state.- Returns:
- The cache state.
- Since:
- 2.6.0.
-
getConnectionFactory
Gets the connection factory.- Returns:
- The connection factory.
- Since:
- 2.6.0.
-
getConnectionIndex
-
getConnectionInitSqls
- Returns:
- The collection of initialization SQL statements.
- Since:
- 2.6.0
-
getDataSourceJmxName
- Returns:
- The data source JMX ObjectName
- Since:
- 2.6.0.
-
getDataSourceJmxObjectName
- Returns:
- The data source JMS ObjectName.
- Since:
- 2.6.0
-
getDefaultAutoCommit
- Returns:
- Default auto-commit value.
- Since:
- 2.6.0
-
getDefaultCatalog
- Returns:
- Default catalog.
- Since:
- 2.6.0
-
getDefaultQueryTimeout
Deprecated.- Returns:
- Default query timeout in seconds.
-
getDefaultQueryTimeoutDuration
Gets the default query timeout Duration.- Returns:
- Default query timeout Duration.
- Since:
- 2.10.0
-
getDefaultQueryTimeoutSeconds
Deprecated.- Returns:
- Default query timeout in seconds.
- Since:
- 2.6.0
-
getDefaultReadOnly
- Returns:
- Default read-only-value.
- Since:
- 2.6.0
-
getDefaultSchema
- Returns:
- Default schema.
- Since:
- 2.6.0
-
getDefaultTransactionIsolation
- Returns:
- Default transaction isolation.
- Since:
- 2.6.0
-
getDisconnectionSqlCodes
SQL_STATE codes considered to signal fatal conditions.Overrides the defaults in
Utils.getDisconnectionSqlCodes()
(plus anything starting withUtils.DISCONNECTION_SQL_CODE_PREFIX
). If this property is non-null andisFastFailValidation()
istrue
, whenever connections created by this factory generate exceptions with SQL_STATE codes in this list, they will be marked as "fatally disconnected" and subsequent validations will fail fast (no attempt at isValid or validation query).If
isFastFailValidation()
isfalse
setting this property has no effect.- Returns:
- SQL_STATE codes overriding defaults
- Since:
- 2.1
-
getMaxConnDuration
Gets the Maximum connection duration.- Returns:
- Maximum connection duration.
- Since:
- 2.10.0
-
getMaxConnLifetimeMillis
Gets the Maximum connection lifetime in milliseconds.- Returns:
- Maximum connection lifetime in milliseconds.
- Since:
- 2.6.0
-
getMaxOpenPreparedStatements
-
getPool
Returns theObjectPool
in whichConnection
s are pooled.- Returns:
- the connection pool
-
getPoolStatements
- Returns:
- Whether to pool statements.
- Since:
- 2.6.0.
-
getValidationQuery
- Returns:
- Validation query.
- Since:
- 2.6.0
-
getValidationQueryTimeoutDuration
Gets the query timeout in seconds.- Returns:
- Validation query timeout in seconds.
- Since:
- 2.10.0
-
getValidationQueryTimeoutSeconds
Deprecated.Gets the query timeout in seconds.- Returns:
- Validation query timeout in seconds.
- Since:
- 2.6.0
-
initializeConnection
- Throws:
SQLException
-
isAutoCommitOnReturn
- Returns:
- Whether to auto-commit on return.
- Since:
- 2.6.0
-
isEnableAutoCommitOnReturn
Deprecated.- Returns:
- Whether to auto-commit on return.
-
isFastFailValidation
True means that validation will fail immediately for connections that have previously thrown SQLExceptions with SQL_STATE indicating fatal disconnection errors.- Returns:
- true if connections created by this factory will fast fail validation.
- Since:
- 2.1, 2.5.0 Defaults to true, previous versions defaulted to false.
- See Also:
-
isRollbackOnReturn
- Returns:
- Whether to rollback on return.
-
makeObject
- Specified by:
makeObject
in interfacePooledObjectFactory<PoolableConnection>
- Throws:
SQLException
-
passivateObject
- Specified by:
passivateObject
in interfacePooledObjectFactory<PoolableConnection>
- Throws:
SQLException
-
setAutoCommitOnReturn
-
setCacheState
-
setClearStatementPoolOnReturn
Sets whether the pool of statements (which was enabled withsetPoolStatements(boolean)
) should be cleared when the connection is returned to its pool. Default is false.- Parameters:
clearStatementPoolOnReturn
- clear or not- Since:
- 2.8.0
-
setConnectionInitSql
Sets the SQL statements I use to initialize newly createdConnection
s. Usingnull
turns off connection initialization.- Parameters:
connectionInitSqls
- SQL statement to initializeConnection
s.
-
setDefaultAutoCommit
Sets the default "auto commit" setting for borrowedConnection
s- Parameters:
defaultAutoCommit
- the default "auto commit" setting for borrowedConnection
s
-
setDefaultCatalog
Sets the default "catalog" setting for borrowedConnection
s- Parameters:
defaultCatalog
- the default "catalog" setting for borrowedConnection
s
-
setDefaultQueryTimeout
Sets the query timeout Duration.- Parameters:
defaultQueryTimeoutDuration
- the query timeout Duration.- Since:
- 2.10.0
-
setDefaultQueryTimeout
Deprecated.Sets the query timeout in seconds.- Parameters:
defaultQueryTimeoutSeconds
- the query timeout in seconds.
-
setDefaultReadOnly
Sets the default "read only" setting for borrowedConnection
s- Parameters:
defaultReadOnly
- the default "read only" setting for borrowedConnection
s
-
setDefaultSchema
Sets the default "schema" setting for borrowedConnection
s- Parameters:
defaultSchema
- the default "schema" setting for borrowedConnection
s- Since:
- 2.5.0
-
setDefaultTransactionIsolation
Sets the default "Transaction Isolation" setting for borrowedConnection
s- Parameters:
defaultTransactionIsolation
- the default "Transaction Isolation" setting for returnedConnection
s
-
setDisconnectionSqlCodes
- Parameters:
disconnectionSqlCodes
- The disconnection SQL codes.- Since:
- 2.1
- See Also:
-
setEnableAutoCommitOnReturn
Deprecated.- Parameters:
autoCommitOnReturn
- Whether to auto-commit on return.
-
setFastFailValidation
- Parameters:
fastFailValidation
- true means connections created by this factory will fast fail validation- Since:
- 2.1
- See Also:
-
setMaxConn
Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation. A value of zero or less indicates an infinite lifetime. The default value is -1.- Parameters:
maxConnDuration
- The maximum lifetime in milliseconds.- Since:
- 2.10.0
-
setMaxConnLifetimeMillis
Deprecated.UsesetMaxConn(Duration)
.Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, passivation and validation. A value of zero or less indicates an infinite lifetime. The default value is -1.- Parameters:
maxConnLifetimeMillis
- The maximum lifetime in milliseconds.
-
setMaxOpenPreparedStatements
Sets the maximum number of open prepared statements.- Parameters:
maxOpenPreparedStatements
- The maximum number of open prepared statements.
-
setMaxOpenPrepatedStatements
Deprecated.Deprecated due to typo in method name.- Parameters:
maxOpenPreparedStatements
- The maximum number of open prepared statements.
-
setPool
Sets theObjectPool
in which to poolConnection
s.- Parameters:
pool
- theObjectPool
in which to pool thoseConnection
s
-
setPoolStatements
-
setRollbackOnReturn
-
setValidationQuery
Sets the query I use tovalidate
Connection
s. Should return at least one row. If not specified,Connection.isValid(int)
will be used to validate connections.- Parameters:
validationQuery
- a query to use tovalidate
Connection
s.
-
setValidationQueryTimeout
Sets the validation query timeout, the amount of time, that connection validation will wait for a response from the database when executing a validation query. Use a value less than or equal to 0 for no timeout.- Parameters:
validationQueryTimeoutDuration
- new validation query timeout duration.- Since:
- 2.10.0
-
setValidationQueryTimeout
Deprecated.Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a response from the database when executing a validation query. Use a value less than or equal to 0 for no timeout.- Parameters:
validationQueryTimeoutSeconds
- new validation query timeout value in seconds
-
validateConnection
Validates the given connection if it is open.- Parameters:
conn
- the connection to validate.- Throws:
SQLException
- if the connection is closed or validate fails.
-
validateObject
- Specified by:
validateObject
in interfacePooledObjectFactory<PoolableConnection>
-
getDefaultQueryTimeoutDuration()
.