public abstract class InstanceKeyDataSource
extends java.lang.Object
implements javax.sql.DataSource, javax.naming.Referenceable, java.io.Serializable, java.lang.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.
|
java.sql.Connection |
getConnection()
Attempts to establish a database connection.
|
java.sql.Connection |
getConnection(java.lang.String userName,
java.lang.String userPassword)
Attempts to retrieve a database connection using
getPooledConnectionAndInfo(String, String) with the
provided user name and password. |
protected abstract org.apache.tomcat.dbcp.dbcp2.datasources.PooledConnectionManager |
getConnectionManager(org.apache.tomcat.dbcp.dbcp2.datasources.UserPassKey upkey) |
javax.sql.ConnectionPoolDataSource |
getConnectionPoolDataSource()
Gets the value of connectionPoolDataSource.
|
java.lang.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. |
java.lang.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. |
long |
getDefaultMaxWaitMillis()
Gets the default value for
BaseObjectPoolConfig.getMaxWaitMillis() for each per user pool. |
long |
getDefaultMinEvictableIdleTimeMillis()
Gets the default value for
BaseObjectPoolConfig.getMinEvictableIdleTimeMillis() 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.
|
java.lang.String |
getDescription()
Gets the description.
|
protected java.lang.String |
getInstanceKey() |
java.lang.String |
getJndiEnvironment(java.lang.String key)
Gets the value of jndiEnvironment which is used when instantiating a JNDI InitialContext.
|
int |
getLoginTimeout()
Gets the value of loginTimeout.
|
java.io.PrintWriter |
getLogWriter()
Gets the value of logWriter.
|
long |
getMaxConnLifetimeMillis()
Returns the maximum permitted lifetime of a connection in milliseconds.
|
java.util.logging.Logger |
getParentLogger() |
protected abstract org.apache.tomcat.dbcp.dbcp2.datasources.PooledConnectionAndInfo |
getPooledConnectionAndInfo(java.lang.String userName,
java.lang.String userPassword) |
java.lang.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.
|
java.lang.Boolean |
isDefaultAutoCommit()
Gets the value of defaultAutoCommit, which defines the state of connections handed out from this pool.
|
java.lang.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(java.lang.Class<?> iface) |
void |
setConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource v)
Sets the backend ConnectionPoolDataSource.
|
void |
setDataSourceName(java.lang.String v)
Sets the name of the ConnectionPoolDataSource which backs this pool.
|
void |
setDefaultAutoCommit(java.lang.Boolean v)
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(java.lang.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 |
setDefaultMaxWaitMillis(long maxWaitMillis)
Sets the default value for
BaseObjectPoolConfig.getMaxWaitMillis() for each per user pool. |
void |
setDefaultMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Sets the default value for
BaseObjectPoolConfig.getMinEvictableIdleTimeMillis() 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(java.lang.Boolean v)
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 v)
Sets the value of defaultTransactionIsolation, which defines the state of connections handed out from this pool.
|
void |
setDescription(java.lang.String v)
Sets the description.
|
void |
setJndiEnvironment(java.lang.String key,
java.lang.String value)
Sets the value of the given JNDI environment property to be used when instantiating a JNDI InitialContext.
|
void |
setLoginTimeout(int v)
Sets the value of loginTimeout.
|
void |
setLogWriter(java.io.PrintWriter v)
Sets the value of logWriter.
|
void |
setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
Sets the maximum permitted lifetime of a connection in milliseconds.
|
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(java.sql.Connection connection,
java.lang.String userName) |
void |
setValidationQuery(java.lang.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 javax.sql.ConnectionPoolDataSource |
testCPDS(java.lang.String userName,
java.lang.String userPassword) |
<T> T |
unwrap(java.lang.Class<T> iface) |
protected static final int UNKNOWN_TRANSACTIONISOLATION
public InstanceKeyDataSource()
protected void assertInitializationAllowed() throws java.lang.IllegalStateException
java.lang.IllegalStateException
public abstract void close() throws java.lang.Exception
close
in interface java.lang.AutoCloseable
java.lang.Exception
protected abstract org.apache.tomcat.dbcp.dbcp2.datasources.PooledConnectionManager getConnectionManager(org.apache.tomcat.dbcp.dbcp2.datasources.UserPassKey upkey)
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException
isWrapperFor
in interface java.sql.Wrapper
java.sql.SQLException
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException
unwrap
in interface java.sql.Wrapper
java.sql.SQLException
public java.util.logging.Logger getParentLogger() throws java.sql.SQLFeatureNotSupportedException
getParentLogger
in interface javax.sql.CommonDataSource
java.sql.SQLFeatureNotSupportedException
public boolean getDefaultBlockWhenExhausted()
BaseObjectPoolConfig.getBlockWhenExhausted()
for each per user pool.BaseObjectPoolConfig.getBlockWhenExhausted()
for each per user
pool.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 java.lang.String getDefaultEvictionPolicyClassName()
BaseObjectPoolConfig.getEvictionPolicyClassName()
for each per user
pool.BaseObjectPoolConfig.getEvictionPolicyClassName()
for each per user
pool.public void setDefaultEvictionPolicyClassName(java.lang.String evictionPolicyClassName)
BaseObjectPoolConfig.getEvictionPolicyClassName()
for each per user
pool.evictionPolicyClassName
- The default value for 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 void setDefaultLifo(boolean lifo)
BaseObjectPoolConfig.getLifo()
for each per user pool.lifo
- The default value for 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 void setDefaultMaxIdle(int maxIdle)
GenericKeyedObjectPoolConfig.getMaxIdlePerKey()
for each per user pool.maxIdle
- The default value for 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 void setDefaultMaxTotal(int maxTotal)
GenericKeyedObjectPoolConfig.getMaxTotalPerKey()
for each per user pool.maxTotal
- The default value for GenericKeyedObjectPoolConfig.getMaxTotalPerKey()
for each per user pool.public long getDefaultMaxWaitMillis()
BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.public void setDefaultMaxWaitMillis(long maxWaitMillis)
BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.maxWaitMillis
- The default value for BaseObjectPoolConfig.getMaxWaitMillis()
for each per user pool.public long getDefaultMinEvictableIdleTimeMillis()
BaseObjectPoolConfig.getMinEvictableIdleTimeMillis()
for each per user
pool.BaseObjectPoolConfig.getMinEvictableIdleTimeMillis()
for each per
user pool.public void setDefaultMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
BaseObjectPoolConfig.getMinEvictableIdleTimeMillis()
for each per user
pool.minEvictableIdleTimeMillis
- The default value for BaseObjectPoolConfig.getMinEvictableIdleTimeMillis()
for each
per user pool.public int getDefaultMinIdle()
GenericKeyedObjectPoolConfig.getMinIdlePerKey()
for each per user pool.GenericKeyedObjectPoolConfig.getMinIdlePerKey()
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 int getDefaultNumTestsPerEvictionRun()
BaseObjectPoolConfig.getNumTestsPerEvictionRun()
for each per user
pool.BaseObjectPoolConfig.getNumTestsPerEvictionRun()
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 long getDefaultSoftMinEvictableIdleTimeMillis()
GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
for each per user pool.GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
for each per user pool.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 boolean getDefaultTestOnCreate()
GenericObjectPool#getTestOnCreate()
for each per user pool.GenericObjectPool#getTestOnCreate()
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 boolean getDefaultTestOnBorrow()
GenericObjectPool#getTestOnBorrow()
for each per user pool.GenericObjectPool#getTestOnBorrow()
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 boolean getDefaultTestOnReturn()
GenericObjectPool#getTestOnReturn()
for each per user pool.GenericObjectPool#getTestOnReturn()
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 boolean getDefaultTestWhileIdle()
GenericObjectPool#getTestWhileIdle()
for each per user pool.GenericObjectPool#getTestWhileIdle()
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 long getDefaultTimeBetweenEvictionRunsMillis()
GenericObjectPool#getTimeBetweenEvictionRunsMillis ()
for each per user pool.GenericObjectPool#getTimeBetweenEvictionRunsMillis ()
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 javax.sql.ConnectionPoolDataSource getConnectionPoolDataSource()
public void setConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource v)
v
- Value to assign to connectionPoolDataSource.public java.lang.String getDataSourceName()
public void setDataSourceName(java.lang.String v)
v
- Value to assign to dataSourceName.public java.lang.Boolean isDefaultAutoCommit()
null
which
will use the default value for the drive.public void setDefaultAutoCommit(java.lang.Boolean v)
null
which
will use the default value for the drive.v
- Value to assign to defaultAutoCommit.public java.lang.Boolean isDefaultReadOnly()
null
which
will use the default value for the drive.public void setDefaultReadOnly(java.lang.Boolean v)
null
which
will use the default value for the drive.v
- Value to assign to defaultReadOnly.public int getDefaultTransactionIsolation()
public void setDefaultTransactionIsolation(int v)
v
- Value to assign to defaultTransactionIsolationpublic java.lang.String getDescription()
public void setDescription(java.lang.String v)
v
- Value to assign to description.protected java.lang.String getInstanceKey()
public java.lang.String getJndiEnvironment(java.lang.String key)
key
- JNDI environment key.public void setJndiEnvironment(java.lang.String key, java.lang.String value)
key
- the JNDI environment property to set.value
- the value assigned to specified JNDI environment property.public int getLoginTimeout()
getLoginTimeout
in interface javax.sql.CommonDataSource
public void setLoginTimeout(int v)
setLoginTimeout
in interface javax.sql.CommonDataSource
v
- Value to assign to loginTimeout.public java.io.PrintWriter getLogWriter()
getLogWriter
in interface javax.sql.CommonDataSource
public void setLogWriter(java.io.PrintWriter v)
setLogWriter
in interface javax.sql.CommonDataSource
v
- Value to assign to logWriter.public java.lang.String getValidationQuery()
Connection.isValid(int)
will be used to validate connections.public void setValidationQuery(java.lang.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 int getValidationQueryTimeout()
public void setValidationQueryTimeout(int validationQueryTimeoutSeconds)
validationQueryTimeoutSeconds
- The new timeout in secondspublic boolean isRollbackAfterValidation()
public void setRollbackAfterValidation(boolean rollbackAfterValidation)
rollbackAfterValidation
- new property valuepublic long getMaxConnLifetimeMillis()
public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis)
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 java.sql.Connection getConnection() throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
java.sql.SQLException
public java.sql.Connection getConnection(java.lang.String userName, java.lang.String userPassword) throws java.sql.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 javax.sql.DataSource
java.sql.SQLException
protected abstract org.apache.tomcat.dbcp.dbcp2.datasources.PooledConnectionAndInfo getPooledConnectionAndInfo(java.lang.String userName, java.lang.String userPassword) throws java.sql.SQLException
java.sql.SQLException
protected abstract void setupDefaults(java.sql.Connection connection, java.lang.String userName) throws java.sql.SQLException
java.sql.SQLException
protected javax.sql.ConnectionPoolDataSource testCPDS(java.lang.String userName, java.lang.String userPassword) throws javax.naming.NamingException, java.sql.SQLException
javax.naming.NamingException
java.sql.SQLException
Copyright © 2000-2018 Apache Software Foundation. All Rights Reserved.