public abstract class InstanceKeyDataSource extends Object implements DataSource, Referenceable, Serializable, AutoCloseable
The base class for SharedPoolDataSource
and PerUserPoolDataSource
. Many of the
configuration properties are shared and defined here. This class is declared public in order to allow particular
usage with commons-beanutils; do not make direct use of it outside of commons-dbcp2.
A J2EE container will normally provide some method of initializing the DataSource
whose attributes are
presented as bean getters/setters and then deploying it via JNDI. It is then available to an application as a source
of pooled logical connections to the database. The pool needs a source of physical connections. This source is in the
form of a ConnectionPoolDataSource
that can be specified via the setDataSourceName(String)
used
to lookup the source via JNDI.
Although normally used within a JNDI environment, A DataSource can be instantiated and initialized as any bean. In
this case the ConnectionPoolDataSource
will likely be instantiated in a similar manner. This class
allows the physical source of connections to be attached directly to this pool using the
setConnectionPoolDataSource(ConnectionPoolDataSource)
method.
The dbcp package contains an adapter, DriverAdapterCPDS
, that can be
used to allow the use of DataSource
's based on this class with JDBC driver implementations that do not
supply a ConnectionPoolDataSource
, but still provide a Driver
implementation.
The package documentation contains an example using Apache Tomcat and JNDI and it also contains a non-JNDI example.
Modifier and Type | Field and Description |
---|---|
protected static int |
UNKNOWN_TRANSACTIONISOLATION
Internal constant to indicate the level is not set.
|
Constructor and Description |
---|
InstanceKeyDataSource()
Default no-arg constructor for Serialization.
|
Modifier and Type | Method and Description |
---|---|
protected void |
assertInitializationAllowed()
Throws an IllegalStateException, if a PooledConnection has already been requested.
|
abstract void |
close()
Closes the connection pool being maintained by this datasource.
|
Connection |
getConnection()
Attempts to establish a database connection.
|
Connection |
getConnection(String userName,
String userPassword)
Attempts to retrieve a database connection using
getPooledConnectionAndInfo(String, String) with the
provided user name and password. |
protected abstract org.apache.commons.dbcp2.datasources.PooledConnectionManager |
getConnectionManager(org.apache.commons.dbcp2.datasources.UserPassKey upkey) |
ConnectionPoolDataSource |
getConnectionPoolDataSource()
Gets the value of connectionPoolDataSource.
|
String |
getDataSourceName()
Gets the name of the ConnectionPoolDataSource which backs this pool.
|
boolean |
getDefaultBlockWhenExhausted()
Gets the default value for
BaseObjectPoolConfig.getBlockWhenExhausted() for each per user pool. |
String |
getDefaultEvictionPolicyClassName()
Gets the default value for
BaseObjectPoolConfig.getEvictionPolicyClassName() for each per user
pool. |
boolean |
getDefaultLifo()
Gets the default value for
BaseObjectPoolConfig.getLifo() for each per user pool. |
int |
getDefaultMaxIdle()
Gets the default value for
GenericKeyedObjectPoolConfig.getMaxIdlePerKey() for each per user pool. |
int |
getDefaultMaxTotal()
Gets the default value for
GenericKeyedObjectPoolConfig.getMaxTotalPerKey() for each per user pool. |
Duration |
getDefaultMaxWait()
Gets the default value for
BaseObjectPoolConfig.getMaxWaitMillis() for each per user pool. |
long |
getDefaultMaxWaitMillis()
Deprecated.
Use
getDefaultMaxWait() . |
long |
getDefaultMinEvictableIdleTimeMillis()
Gets the default value for
BaseObjectPoolConfig.getMinEvictableIdleTime() for each per user
pool. |
int |
getDefaultMinIdle()
Gets the default value for
GenericKeyedObjectPoolConfig.getMinIdlePerKey() for each per user pool. |
int |
getDefaultNumTestsPerEvictionRun()
Gets the default value for
BaseObjectPoolConfig.getNumTestsPerEvictionRun() for each per user
pool. |
long |
getDefaultSoftMinEvictableIdleTimeMillis()
Gets the default value for
GenericObjectPool#getSoftMinEvictableIdleTimeMillis() for each per user pool. |
boolean |
getDefaultTestOnBorrow()
Gets the default value for
GenericObjectPool#getTestOnBorrow() for each per user pool. |
boolean |
getDefaultTestOnCreate()
Gets the default value for
GenericObjectPool#getTestOnCreate() for each per user pool. |
boolean |
getDefaultTestOnReturn()
Gets the default value for
GenericObjectPool#getTestOnReturn() for each per user pool. |
boolean |
getDefaultTestWhileIdle()
Gets the default value for
GenericObjectPool#getTestWhileIdle() for each per user pool. |
long |
getDefaultTimeBetweenEvictionRunsMillis()
Gets the default value for
GenericObjectPool#getTimeBetweenEvictionRunsMillis () for each per user pool. |
int |
getDefaultTransactionIsolation()
Gets the value of defaultTransactionIsolation, which defines the state of connections handed out from this pool.
|
String |
getDescription()
Gets the description.
|
protected String |
getInstanceKey() |
String |
getJndiEnvironment(String key)
Gets the value of jndiEnvironment which is used when instantiating a JNDI InitialContext.
|
int |
getLoginTimeout()
Gets the value of loginTimeout.
|
PrintWriter |
getLogWriter()
Gets the value of logWriter.
|
Duration |
getMaxConnLifetime()
Gets the maximum permitted lifetime of a connection.
|
long |
getMaxConnLifetimeMillis()
Deprecated.
Use
getMaxConnLifetime() . |
Logger |
getParentLogger() |
protected abstract org.apache.commons.dbcp2.datasources.PooledConnectionAndInfo |
getPooledConnectionAndInfo(String userName,
String userPassword)
This method is protected but can only be implemented in this package because PooledConnectionAndInfo is a package
private type.
|
String |
getValidationQuery()
Gets the SQL query that will be used to validate connections from this pool before returning them to the caller.
|
int |
getValidationQueryTimeout()
Returns the timeout in seconds before the validation query fails.
|
Boolean |
isDefaultAutoCommit()
Gets the value of defaultAutoCommit, which defines the state of connections handed out from this pool.
|
Boolean |
isDefaultReadOnly()
Gets the value of defaultReadOnly, which defines the state of connections handed out from this pool.
|
boolean |
isRollbackAfterValidation()
Whether a rollback will be issued after executing the SQL query that will be used to validate connections from
this pool before returning them to the caller.
|
boolean |
isWrapperFor(Class<?> iface) |
void |
setConnectionPoolDataSource(ConnectionPoolDataSource dataSource)
Sets the back end ConnectionPoolDataSource.
|
void |
setDataSourceName(String dataSourceName)
Sets the name of the ConnectionPoolDataSource which backs this pool.
|
void |
setDefaultAutoCommit(Boolean defaultAutoCommit)
Sets the value of defaultAutoCommit, which defines the state of connections handed out from this pool.
|
void |
setDefaultBlockWhenExhausted(boolean blockWhenExhausted)
Sets the default value for
BaseObjectPoolConfig.getBlockWhenExhausted() for each per user pool. |
void |
setDefaultEvictionPolicyClassName(String evictionPolicyClassName)
Sets the default value for
BaseObjectPoolConfig.getEvictionPolicyClassName() for each per user
pool. |
void |
setDefaultLifo(boolean lifo)
Sets the default value for
BaseObjectPoolConfig.getLifo() for each per user pool. |
void |
setDefaultMaxIdle(int maxIdle)
Sets the default value for
GenericKeyedObjectPoolConfig.getMaxIdlePerKey() for each per user pool. |
void |
setDefaultMaxTotal(int maxTotal)
Sets the default value for
GenericKeyedObjectPoolConfig.getMaxTotalPerKey() for each per user pool. |
void |
setDefaultMaxWait(Duration maxWaitMillis)
Sets the default value for
BaseObjectPoolConfig.getMaxWaitMillis() for each per user pool. |
void |
setDefaultMaxWaitMillis(long maxWaitMillis)
Deprecated.
|
void |
setDefaultMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Sets the default value for
BaseObjectPoolConfig.getMinEvictableIdleTime() for each per user
pool. |
void |
setDefaultMinIdle(int minIdle)
Sets the default value for
GenericKeyedObjectPoolConfig.getMinIdlePerKey() for each per user pool. |
void |
setDefaultNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the default value for
BaseObjectPoolConfig.getNumTestsPerEvictionRun() for each per user
pool. |
void |
setDefaultReadOnly(Boolean defaultReadOnly)
Sets the value of defaultReadOnly, which defines the state of connections handed out from this pool.
|
void |
setDefaultSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
Sets the default value for
GenericObjectPool#getSoftMinEvictableIdleTimeMillis() for each per user pool. |
void |
setDefaultTestOnBorrow(boolean testOnBorrow)
Sets the default value for
GenericObjectPool#getTestOnBorrow() for each per user pool. |
void |
setDefaultTestOnCreate(boolean testOnCreate)
Sets the default value for
GenericObjectPool#getTestOnCreate() for each per user pool. |
void |
setDefaultTestOnReturn(boolean testOnReturn)
Sets the default value for
GenericObjectPool#getTestOnReturn() for each per user pool. |
void |
setDefaultTestWhileIdle(boolean testWhileIdle)
Sets the default value for
GenericObjectPool#getTestWhileIdle() for each per user pool. |
void |
setDefaultTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
Sets the default value for
GenericObjectPool#getTimeBetweenEvictionRunsMillis () for each per user pool. |
void |
setDefaultTransactionIsolation(int defaultTransactionIsolation)
Sets the value of defaultTransactionIsolation, which defines the state of connections handed out from this pool.
|
void |
setDescription(String description)
Sets the description.
|
void |
setJndiEnvironment(String key,
String value)
Sets the value of the given JNDI environment property to be used when instantiating a JNDI InitialContext.
|
void |
setLoginTimeout(int loginTimeout)
Sets the value of loginTimeout.
|
void |
setLogWriter(PrintWriter logWriter)
Sets the value of logWriter.
|
void |
setMaxConnLifetime(Duration maxConnLifetimeMillis)
Sets the maximum permitted lifetime of a connection.
|
void |
setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
Deprecated.
|
void |
setRollbackAfterValidation(boolean rollbackAfterValidation)
Whether a rollback will be issued after executing the SQL query that will be used to validate connections from
this pool before returning them to the caller.
|
protected abstract void |
setupDefaults(Connection connection,
String userName) |
void |
setValidationQuery(String validationQuery)
Sets the SQL query that will be used to validate connections from this pool before returning them to the caller.
|
void |
setValidationQueryTimeout(int validationQueryTimeoutSeconds)
Sets the timeout in seconds before the validation query fails.
|
protected ConnectionPoolDataSource |
testCPDS(String userName,
String userPassword) |
String |
toString() |
protected void |
toStringFields(StringBuilder builder) |
<T> T |
unwrap(Class<T> iface) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getReference
protected static final int UNKNOWN_TRANSACTIONISOLATION
public InstanceKeyDataSource()
protected void assertInitializationAllowed() throws IllegalStateException
IllegalStateException
- Thrown if a PooledConnection has already been requested.public abstract void close() throws Exception
close
in interface AutoCloseable
Exception
public Connection getConnection() throws SQLException
getConnection
in interface DataSource
SQLException
public Connection getConnection(String userName, String userPassword) throws SQLException
getPooledConnectionAndInfo(String, String)
with the
provided user name and password. The password on the PooledConnectionAndInfo
instance returned by
getPooledConnectionAndInfo
is compared to the password
parameter. If the comparison
fails, a database connection using the supplied user name and password is attempted. If the connection attempt
fails, an SQLException is thrown, indicating that the given password did not match the password used to create
the pooled connection. If the connection attempt succeeds, this means that the database password has been
changed. In this case, the PooledConnectionAndInfo
instance retrieved with the old password is
destroyed and the getPooledConnectionAndInfo
is repeatedly invoked until a
PooledConnectionAndInfo
instance with the new password is returned.getConnection
in interface DataSource
SQLException
protected abstract org.apache.commons.dbcp2.datasources.PooledConnectionManager getConnectionManager(org.apache.commons.dbcp2.datasources.UserPassKey upkey)
public ConnectionPoolDataSource getConnectionPoolDataSource()
public String getDataSourceName()
public boolean getDefaultBlockWhenExhausted()
BaseObjectPoolConfig.getBlockWhenExhausted()
for each per user pool.BaseObjectPoolConfig.getBlockWhenExhausted()
for each per user
pool.public String getDefaultEvictionPolicyClassName()
BaseObjectPoolConfig.getEvictionPolicyClassName()
for each per user
pool.BaseObjectPoolConfig.getEvictionPolicyClassName()
for each per user
pool.public boolean getDefaultLifo()
BaseObjectPoolConfig.getLifo()
for each per user pool.BaseObjectPoolConfig.getLifo()
for each per user pool.public int getDefaultMaxIdle()
GenericKeyedObjectPoolConfig.getMaxIdlePerKey()
for each per user pool.GenericKeyedObjectPoolConfig.getMaxIdlePerKey()
for each per user pool.public int getDefaultMaxTotal()
GenericKeyedObjectPoolConfig.getMaxTotalPerKey()
for each per user pool.GenericKeyedObjectPoolConfig.getMaxTotalPerKey()
for each per user pool.public Duration getDefaultMaxWait()
BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.@Deprecated public long getDefaultMaxWaitMillis()
getDefaultMaxWait()
.BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.public long getDefaultMinEvictableIdleTimeMillis()
BaseObjectPoolConfig.getMinEvictableIdleTime()
for each per user
pool.BaseObjectPoolConfig.getMinEvictableIdleTime()
for each per
user pool.public int getDefaultMinIdle()
GenericKeyedObjectPoolConfig.getMinIdlePerKey()
for each per user pool.GenericKeyedObjectPoolConfig.getMinIdlePerKey()
for each per user pool.public int getDefaultNumTestsPerEvictionRun()
BaseObjectPoolConfig.getNumTestsPerEvictionRun()
for each per user
pool.BaseObjectPoolConfig.getNumTestsPerEvictionRun()
for each per user
pool.public long getDefaultSoftMinEvictableIdleTimeMillis()
GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
for each per user pool.GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
for each per user pool.public boolean getDefaultTestOnBorrow()
GenericObjectPool#getTestOnBorrow()
for each per user pool.GenericObjectPool#getTestOnBorrow()
for each per user pool.public boolean getDefaultTestOnCreate()
GenericObjectPool#getTestOnCreate()
for each per user pool.GenericObjectPool#getTestOnCreate()
for each per user pool.public boolean getDefaultTestOnReturn()
GenericObjectPool#getTestOnReturn()
for each per user pool.GenericObjectPool#getTestOnReturn()
for each per user pool.public boolean getDefaultTestWhileIdle()
GenericObjectPool#getTestWhileIdle()
for each per user pool.GenericObjectPool#getTestWhileIdle()
for each per user pool.public long getDefaultTimeBetweenEvictionRunsMillis()
GenericObjectPool#getTimeBetweenEvictionRunsMillis ()
for each per user pool.GenericObjectPool#getTimeBetweenEvictionRunsMillis ()
for each per user pool.public int getDefaultTransactionIsolation()
public String getDescription()
protected String getInstanceKey()
public String getJndiEnvironment(String key)
key
- JNDI environment key.public int getLoginTimeout()
getLoginTimeout
in interface CommonDataSource
public PrintWriter getLogWriter()
getLogWriter
in interface CommonDataSource
public Duration getMaxConnLifetime()
@Deprecated public long getMaxConnLifetimeMillis()
getMaxConnLifetime()
.public Logger getParentLogger() throws SQLFeatureNotSupportedException
getParentLogger
in interface CommonDataSource
SQLFeatureNotSupportedException
protected abstract org.apache.commons.dbcp2.datasources.PooledConnectionAndInfo getPooledConnectionAndInfo(String userName, String userPassword) throws SQLException
userName
- The user name.userPassword
- The user password.SQLException
- Connection or registration failure.public String getValidationQuery()
Connection.isValid(int)
will be used to validate connections.public int getValidationQueryTimeout()
public Boolean isDefaultAutoCommit()
null
which
will use the default value for the drive.public Boolean isDefaultReadOnly()
null
which
will use the default value for the drive.public boolean isRollbackAfterValidation()
public boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor
in interface Wrapper
SQLException
public void setConnectionPoolDataSource(ConnectionPoolDataSource dataSource)
dataSource
- Value to assign to connectionPoolDataSource.public void setDataSourceName(String dataSourceName)
dataSourceName
- Value to assign to dataSourceName.public void setDefaultAutoCommit(Boolean defaultAutoCommit)
null
which
will use the default value for the drive.defaultAutoCommit
- Value to assign to defaultAutoCommit.public void setDefaultBlockWhenExhausted(boolean blockWhenExhausted)
BaseObjectPoolConfig.getBlockWhenExhausted()
for each per user pool.blockWhenExhausted
- The default value for BaseObjectPoolConfig.getBlockWhenExhausted()
for each per user
pool.public void setDefaultEvictionPolicyClassName(String evictionPolicyClassName)
BaseObjectPoolConfig.getEvictionPolicyClassName()
for each per user
pool.evictionPolicyClassName
- The default value for BaseObjectPoolConfig.getEvictionPolicyClassName()
for each per
user pool.public void setDefaultLifo(boolean lifo)
BaseObjectPoolConfig.getLifo()
for each per user pool.lifo
- The default value for BaseObjectPoolConfig.getLifo()
for each per user pool.public void setDefaultMaxIdle(int maxIdle)
GenericKeyedObjectPoolConfig.getMaxIdlePerKey()
for each per user pool.maxIdle
- The default value for GenericKeyedObjectPoolConfig.getMaxIdlePerKey()
for each per user pool.public void setDefaultMaxTotal(int maxTotal)
GenericKeyedObjectPoolConfig.getMaxTotalPerKey()
for each per user pool.maxTotal
- The default value for GenericKeyedObjectPoolConfig.getMaxTotalPerKey()
for each per user pool.public void setDefaultMaxWait(Duration maxWaitMillis)
BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.maxWaitMillis
- The default value for BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.@Deprecated public void setDefaultMaxWaitMillis(long maxWaitMillis)
setDefaultMaxWait(Duration)
.BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.maxWaitMillis
- The default value for BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.public void setDefaultMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
BaseObjectPoolConfig.getMinEvictableIdleTime()
for each per user
pool.minEvictableIdleTimeMillis
- The default value for BaseObjectPoolConfig.getMinEvictableIdleTime()
for each
per user pool.public void setDefaultMinIdle(int minIdle)
GenericKeyedObjectPoolConfig.getMinIdlePerKey()
for each per user pool.minIdle
- The default value for GenericKeyedObjectPoolConfig.getMinIdlePerKey()
for each per user pool.public void setDefaultNumTestsPerEvictionRun(int numTestsPerEvictionRun)
BaseObjectPoolConfig.getNumTestsPerEvictionRun()
for each per user
pool.numTestsPerEvictionRun
- The default value for BaseObjectPoolConfig.getNumTestsPerEvictionRun()
for each per
user pool.public void setDefaultReadOnly(Boolean defaultReadOnly)
null
which
will use the default value for the drive.defaultReadOnly
- Value to assign to defaultReadOnly.public void setDefaultSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
for each per user pool.softMinEvictableIdleTimeMillis
- The default value for GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
for each per user pool.public void setDefaultTestOnBorrow(boolean testOnBorrow)
GenericObjectPool#getTestOnBorrow()
for each per user pool.testOnBorrow
- The default value for GenericObjectPool#getTestOnBorrow()
for each per user pool.public void setDefaultTestOnCreate(boolean testOnCreate)
GenericObjectPool#getTestOnCreate()
for each per user pool.testOnCreate
- The default value for GenericObjectPool#getTestOnCreate()
for each per user pool.public void setDefaultTestOnReturn(boolean testOnReturn)
GenericObjectPool#getTestOnReturn()
for each per user pool.testOnReturn
- The default value for GenericObjectPool#getTestOnReturn()
for each per user pool.public void setDefaultTestWhileIdle(boolean testWhileIdle)
GenericObjectPool#getTestWhileIdle()
for each per user pool.testWhileIdle
- The default value for GenericObjectPool#getTestWhileIdle()
for each per user pool.public void setDefaultTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
GenericObjectPool#getTimeBetweenEvictionRunsMillis ()
for each per user pool.timeBetweenEvictionRunsMillis
- The default value for GenericObjectPool#getTimeBetweenEvictionRunsMillis ()
for each per user pool.public void setDefaultTransactionIsolation(int defaultTransactionIsolation)
defaultTransactionIsolation
- Value to assign to defaultTransactionIsolationpublic void setDescription(String description)
description
- Value to assign to description.public void setJndiEnvironment(String key, String value)
key
- the JNDI environment property to set.value
- the value assigned to specified JNDI environment property.public void setLoginTimeout(int loginTimeout)
setLoginTimeout
in interface CommonDataSource
loginTimeout
- Value to assign to loginTimeout.public void setLogWriter(PrintWriter logWriter)
setLogWriter
in interface CommonDataSource
logWriter
- Value to assign to logWriter.public void setMaxConnLifetime(Duration maxConnLifetimeMillis)
Sets the maximum permitted lifetime of a connection. A value of zero or less indicates an infinite lifetime.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
maxConnLifetimeMillis
- The maximum permitted lifetime of a connection. A value of zero or less indicates an
infinite lifetime.@Deprecated public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
setMaxConnLifetime(Duration)
.Sets the maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an infinite lifetime.
Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
time one of the following methods is invoked: getConnection, setLogwriter,
setLoginTimeout, getLoginTimeout, getLogWriter.
maxConnLifetimeMillis
- The maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an
infinite lifetime.public void setRollbackAfterValidation(boolean rollbackAfterValidation)
rollbackAfterValidation
- new property valueprotected abstract void setupDefaults(Connection connection, String userName) throws SQLException
SQLException
public void setValidationQuery(String validationQuery)
Connection.isValid(int)
.validationQuery
- The SQL query that will be used to validate connections from this pool before returning them to the
caller.public void setValidationQueryTimeout(int validationQueryTimeoutSeconds)
validationQueryTimeoutSeconds
- The new timeout in secondsprotected ConnectionPoolDataSource testCPDS(String userName, String userPassword) throws NamingException, SQLException
NamingException
SQLException
protected void toStringFields(StringBuilder builder)
public <T> T unwrap(Class<T> iface) throws SQLException
unwrap
in interface Wrapper
SQLException
Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.