java.io.Serializable
, java.lang.AutoCloseable
, java.sql.Wrapper
, javax.naming.Referenceable
, javax.sql.CommonDataSource
, javax.sql.DataSource
PerUserPoolDataSource
, SharedPoolDataSource
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 | Description |
---|---|---|
protected static int |
UNKNOWN_TRANSACTIONISOLATION |
Internal constant to indicate the level is not set.
|
Constructor | Description |
---|---|
InstanceKeyDataSource() |
Default no-arg constructor for Serialization
|
Modifier and Type | Method | Description |
---|---|---|
protected void |
assertInitializationAllowed() |
Throws an IllegalStateException, if a PooledConnection has already
been requested.
|
abstract void |
close() |
Close the connection pool being maintained by this datasource.
|
java.sql.Connection |
getConnection() |
Attempt to establish a database connection.
|
java.sql.Connection |
getConnection(java.lang.String username,
java.lang.String password) |
Attempt to retrieve a database connection using
getPooledConnectionAndInfo(String, String)
with the provided username and password. |
protected abstract org.apache.tomcat.dbcp.dbcp2.datasources.PooledConnectionManager |
getConnectionManager(org.apache.tomcat.dbcp.dbcp2.datasources.UserPassKey upkey) |
|
javax.sql.ConnectionPoolDataSource |
getConnectionPoolDataSource() |
Get the value of connectionPoolDataSource.
|
java.lang.String |
getDataSourceName() |
Get the name of the ConnectionPoolDataSource which backs this pool.
|
boolean |
getDefaultBlockWhenExhausted() |
|
java.lang.String |
getDefaultEvictionPolicyClassName() |
|
boolean |
getDefaultLifo() |
|
int |
getDefaultMaxIdle() |
|
int |
getDefaultMaxTotal() |
|
long |
getDefaultMaxWaitMillis() |
|
long |
getDefaultMinEvictableIdleTimeMillis() |
|
int |
getDefaultMinIdle() |
|
int |
getDefaultNumTestsPerEvictionRun() |
|
long |
getDefaultSoftMinEvictableIdleTimeMillis() |
|
boolean |
getDefaultTestOnBorrow() |
|
boolean |
getDefaultTestOnCreate() |
|
boolean |
getDefaultTestOnReturn() |
|
boolean |
getDefaultTestWhileIdle() |
|
long |
getDefaultTimeBetweenEvictionRunsMillis() |
|
int |
getDefaultTransactionIsolation() |
Get the value of defaultTransactionIsolation, which defines the state of
connections handed out from this pool.
|
java.lang.String |
getDescription() |
Get the description.
|
protected java.lang.String |
getInstanceKey() |
|
java.lang.String |
getJndiEnvironment(java.lang.String key) |
Get the value of jndiEnvironment which is used when instantiating
a jndi InitialContext.
|
int |
getLoginTimeout() |
Get the value of loginTimeout.
|
java.io.PrintWriter |
getLogWriter() |
Get the value of logWriter.
|
long |
getMaxConnLifetimeMillis() |
|
java.util.logging.Logger |
getParentLogger() |
|
protected abstract org.apache.tomcat.dbcp.dbcp2.datasources.PooledConnectionAndInfo |
getPooledConnectionAndInfo(java.lang.String username,
java.lang.String password) |
|
java.lang.String |
getValidationQuery() |
The SQL query that will be used to validate connections from this pool
before returning them to the caller.
|
int |
getValidationQueryTimeout() |
|
java.lang.Boolean |
isDefaultAutoCommit() |
Get the value of defaultAutoCommit, which defines the state of
connections handed out from this pool.
|
java.lang.Boolean |
isDefaultReadOnly() |
Get 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) |
Set the backend ConnectionPoolDataSource.
|
void |
setDataSourceName(java.lang.String v) |
Set the name of the ConnectionPoolDataSource which backs this pool.
|
void |
setDefaultAutoCommit(java.lang.Boolean v) |
Set 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) |
Set 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) |
Set the value of defaultTransactionIsolation, which defines the state of
connections handed out from this pool.
|
void |
setDescription(java.lang.String v) |
Set 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) |
Set the value of loginTimeout.
|
void |
setLogWriter(java.io.PrintWriter v) |
Set 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 con,
java.lang.String username) |
|
void |
setValidationQuery(java.lang.String validationQuery) |
The SQL query that will be used to validate connections from this pool
before returning them to the caller.
|
void |
setValidationQueryTimeout(int validationQueryTimeout) |
Sets the timeout in seconds before the validation query fails.
|
protected javax.sql.ConnectionPoolDataSource |
testCPDS(java.lang.String username,
java.lang.String password) |
|
<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.public void setDefaultBlockWhenExhausted(boolean blockWhenExhausted)
BaseObjectPoolConfig.getBlockWhenExhausted()
for each per
user pool.blockWhenExhausted
- The new valuepublic java.lang.String getDefaultEvictionPolicyClassName()
BaseObjectPoolConfig.getEvictionPolicyClassName()
for
each per user pool.public void setDefaultEvictionPolicyClassName(java.lang.String evictionPolicyClassName)
BaseObjectPoolConfig.getEvictionPolicyClassName()
for
each per user pool.evictionPolicyClassName
- The new valuepublic boolean getDefaultLifo()
BaseObjectPoolConfig.getLifo()
for each per user pool.public void setDefaultLifo(boolean lifo)
BaseObjectPoolConfig.getLifo()
for each per user pool.lifo
- The new valuepublic int getDefaultMaxIdle()
GenericKeyedObjectPoolConfig.getMaxIdlePerKey()
for each per user
pool.public void setDefaultMaxIdle(int maxIdle)
GenericKeyedObjectPoolConfig.getMaxIdlePerKey()
for each per user
pool.maxIdle
- The new valuepublic int getDefaultMaxTotal()
GenericKeyedObjectPoolConfig.getMaxTotalPerKey()
for each per
user pool.public void setDefaultMaxTotal(int maxTotal)
GenericKeyedObjectPoolConfig.getMaxTotalPerKey()
for each per
user pool.maxTotal
- The new valuepublic long getDefaultMaxWaitMillis()
BaseObjectPoolConfig.getMaxWaitMillis()
for each per user
pool.public void setDefaultMaxWaitMillis(long maxWaitMillis)
BaseObjectPoolConfig.getMaxWaitMillis()
for each per user
pool.maxWaitMillis
- The new valuepublic long getDefaultMinEvictableIdleTimeMillis()
BaseObjectPoolConfig.getMinEvictableIdleTimeMillis()
for
each per user pool.public void setDefaultMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
BaseObjectPoolConfig.getMinEvictableIdleTimeMillis()
for
each per user pool.minEvictableIdleTimeMillis
- The new valuepublic int getDefaultMinIdle()
GenericKeyedObjectPoolConfig.getMinIdlePerKey()
for each per user
pool.public void setDefaultMinIdle(int minIdle)
GenericKeyedObjectPoolConfig.getMinIdlePerKey()
for each per user
pool.minIdle
- The new valuepublic int getDefaultNumTestsPerEvictionRun()
BaseObjectPoolConfig.getNumTestsPerEvictionRun()
for each
per user pool.public void setDefaultNumTestsPerEvictionRun(int numTestsPerEvictionRun)
BaseObjectPoolConfig.getNumTestsPerEvictionRun()
for each
per user pool.numTestsPerEvictionRun
- The new valuepublic long getDefaultSoftMinEvictableIdleTimeMillis()
GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
for each
per user pool.public void setDefaultSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
GenericObjectPool#getSoftMinEvictableIdleTimeMillis()
for each per user pool.softMinEvictableIdleTimeMillis
- The new valuepublic boolean getDefaultTestOnCreate()
GenericObjectPool#getTestOnCreate()
for each per user pool.public void setDefaultTestOnCreate(boolean testOnCreate)
GenericObjectPool#getTestOnCreate()
for each per user pool.testOnCreate
- The new valuepublic boolean getDefaultTestOnBorrow()
GenericObjectPool#getTestOnBorrow()
for each per user pool.public void setDefaultTestOnBorrow(boolean testOnBorrow)
GenericObjectPool#getTestOnBorrow()
for each per user pool.testOnBorrow
- The new valuepublic boolean getDefaultTestOnReturn()
GenericObjectPool#getTestOnReturn()
for each per user pool.public void setDefaultTestOnReturn(boolean testOnReturn)
GenericObjectPool#getTestOnReturn()
for each per user pool.testOnReturn
- The new valuepublic boolean getDefaultTestWhileIdle()
GenericObjectPool#getTestWhileIdle()
for each per user pool.public void setDefaultTestWhileIdle(boolean testWhileIdle)
GenericObjectPool#getTestWhileIdle()
for each per user pool.testWhileIdle
- The new valuepublic long getDefaultTimeBetweenEvictionRunsMillis()
GenericObjectPool#getTimeBetweenEvictionRunsMillis ()
for each
per user pool.public void setDefaultTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
GenericObjectPool#getTimeBetweenEvictionRunsMillis ()
for each
per user pool.timeBetweenEvictionRunsMillis
- The new valuepublic 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
- The environment property namepublic 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
getLoginTimeout
in interface javax.sql.DataSource
public void setLoginTimeout(int v)
setLoginTimeout
in interface javax.sql.CommonDataSource
setLoginTimeout
in interface javax.sql.DataSource
v
- Value to assign to loginTimeout.public java.io.PrintWriter getLogWriter()
getLogWriter
in interface javax.sql.CommonDataSource
getLogWriter
in interface javax.sql.DataSource
public void setLogWriter(java.io.PrintWriter v)
setLogWriter
in interface javax.sql.CommonDataSource
setLogWriter
in interface javax.sql.DataSource
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 validation querypublic int getValidationQueryTimeout()
public void setValidationQueryTimeout(int validationQueryTimeout)
validationQueryTimeout
- 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 connection lifetimepublic java.sql.Connection getConnection() throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
java.sql.SQLException
- Connection failedpublic java.sql.Connection getConnection(java.lang.String username, java.lang.String password) throws java.sql.SQLException
getPooledConnectionAndInfo(String, String)
with the provided username 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 username 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
username
- The user name to use to connectpassword
- The passwordjava.sql.SQLException
- Connection failedprotected abstract org.apache.tomcat.dbcp.dbcp2.datasources.PooledConnectionAndInfo getPooledConnectionAndInfo(java.lang.String username, java.lang.String password) throws java.sql.SQLException
java.sql.SQLException
protected abstract void setupDefaults(java.sql.Connection con, java.lang.String username) throws java.sql.SQLException
java.sql.SQLException
protected javax.sql.ConnectionPoolDataSource testCPDS(java.lang.String username, java.lang.String password) throws javax.naming.NamingException, java.sql.SQLException
javax.naming.NamingException
java.sql.SQLException
Copyright © 2000-2018 Apache Software Foundation. All Rights Reserved.