| Method from org.apache.commons.dbcp.datasources.InstanceKeyDataSource Detail: |
protected void assertInitializationAllowed() throws IllegalStateException {
if (getConnectionCalled) {
throw new IllegalStateException(GET_CONNECTION_CALLED);
}
}
Throws an IllegalStateException, if a PooledConnection has already
been requested. |
abstract public void close() throws Exception
Close pool being maintained by this datasource. |
public Connection getConnection() throws SQLException {
return getConnection(null, null);
}
Attempt to establish a database connection. |
public Connection getConnection(String username,
String password) throws SQLException {
if (instanceKey == null) {
throw new SQLException("Must set the ConnectionPoolDataSource "
+ "through setDataSourceName or setConnectionPoolDataSource"
+ " before calling getConnection.");
}
getConnectionCalled = true;
PooledConnectionAndInfo info = null;
try {
info = getPooledConnectionAndInfo(username, password);
} catch (NoSuchElementException e) {
closeDueToException(info);
throw new SQLNestedException("Cannot borrow connection from pool", e);
} catch (RuntimeException e) {
closeDueToException(info);
throw e;
} catch (SQLException e) {
closeDueToException(info);
throw e;
} catch (Exception e) {
closeDueToException(info);
throw new SQLNestedException("Cannot borrow connection from pool", e);
}
if (!(null == password ? null == info.getPassword()
: password.equals(info.getPassword()))) {
closeDueToException(info);
throw new SQLException("Given password did not match password used"
+ " to create the PooledConnection.");
}
Connection con = info.getPooledConnection().getConnection();
setupDefaults(con, username);
con.clearWarnings();
return con;
}
Attempt to establish a database connection. |
public ConnectionPoolDataSource getConnectionPoolDataSource() {
return cpds;
}
Get the value of connectionPoolDataSource. This method will return
null, if the backing datasource is being accessed via jndi. |
public String getDataSourceName() {
return dataSourceName;
}
Get the name of the ConnectionPoolDataSource which backs this pool.
This name is used to look up the datasource from a jndi service
provider. |
public int getDefaultTransactionIsolation() {
return defaultTransactionIsolation;
}
Get the value of defaultTransactionIsolation, which defines the state of
connections handed out from this pool. The value can be changed
on the Connection using Connection.setTransactionIsolation(int).
If this method returns -1, the default is JDBC driver dependent. |
public String getDescription() {
return description;
}
Get the description. This property is defined by jdbc as for use with
GUI (or other) tools that might deploy the datasource. It serves no
internal purpose. |
public String getJndiEnvironment(String key) {
String value = null;
if (jndiEnvironment != null) {
value = jndiEnvironment.getProperty(key);
}
return value;
}
Get the value of jndiEnvironment which is used when instantiating
a jndi InitialContext. This InitialContext is used to locate the
backend ConnectionPoolDataSource. |
public PrintWriter getLogWriter() {
if (logWriter == null) {
logWriter = new PrintWriter(System.out);
}
return logWriter;
}
Get the value of logWriter. |
public int getLoginTimeout() {
return loginTimeout;
}
Get the value of loginTimeout. |
public int getMinEvictableIdleTimeMillis() {
return _minEvictableIdleTimeMillis;
}
Returns the minimum amount of time an object may sit idle in the pool
before it is eligable for eviction by the idle object evictor
(if any). |
public int getNumTestsPerEvictionRun() {
return _numTestsPerEvictionRun;
}
Returns the number of objects to examine during each run of the
idle object evictor thread (if any). |
abstract protected PooledConnectionAndInfo getPooledConnectionAndInfo(String username,
String password) throws SQLException
|
public Reference getReference() throws NamingException {
final String className = getClass().getName();
final String factoryName = className + "Factory"; // XXX: not robust
Reference ref = new Reference(className, factoryName, null);
ref.add(new StringRefAddr("instanceKey", instanceKey));
return ref;
}
Retrieves the Reference of this object.
Note: InstanceKeyDataSource subclasses
should override this method. The implementaion included below
is not robust and will be removed at the next major version DBCP
release. |
public boolean getTestOnBorrow() {
return _testOnBorrow;
}
When true, objects will be
{*link PoolableObjectFactory#validateObject validated}
before being returned by the {*link #borrowObject}
method. If the object fails to validate,
it will be dropped from the pool, and we will attempt
to borrow another. |
public boolean getTestOnReturn() {
return _testOnReturn;
}
When true, objects will be
{*link PoolableObjectFactory#validateObject validated}
before being returned to the pool within the
{*link #returnObject}. |
public boolean getTestWhileIdle() {
return _testWhileIdle;
}
When true, objects will be
{*link PoolableObjectFactory#validateObject validated}
by the idle object evictor (if any). If an object
fails to validate, it will be dropped from the pool. |
public int getTimeBetweenEvictionRunsMillis() {
return _timeBetweenEvictionRunsMillis;
}
Returns the number of milliseconds to sleep between runs of the
idle object evictor thread.
When non-positive, no idle object evictor thread will be
run. |
public String getValidationQuery() {
return (this.validationQuery);
}
The SQL query that will be used to validate connections from this pool
before returning them to the caller. If specified, this query
MUST be an SQL SELECT statement that returns at least
one row. |
public boolean isDefaultAutoCommit() {
return defaultAutoCommit;
}
Get the value of defaultAutoCommit, which defines the state of
connections handed out from this pool. The value can be changed
on the Connection using Connection.setAutoCommit(boolean).
The default is true. |
public boolean isDefaultReadOnly() {
return defaultReadOnly;
}
Get the value of defaultReadOnly, which defines the state of
connections handed out from this pool. The value can be changed
on the Connection using Connection.setReadOnly(boolean).
The default is false. |
public boolean isRollbackAfterValidation() {
return (this.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. |
public final boolean isTestOnBorrow() {
return getTestOnBorrow();
}
|
public final boolean isTestOnReturn() {
return getTestOnReturn();
}
|
public final boolean isTestWhileIdle() {
return getTestWhileIdle();
}
|
public void setConnectionPoolDataSource(ConnectionPoolDataSource v) {
assertInitializationAllowed();
if (dataSourceName != null) {
throw new IllegalStateException(
"Cannot set the DataSource, if JNDI is used.");
}
if (cpds != null)
{
throw new IllegalStateException(
"The CPDS has already been set. It cannot be altered.");
}
cpds = v;
instanceKey = InstanceKeyObjectFactory.registerNewInstance(this);
}
Set the backend ConnectionPoolDataSource. This property should not be
set if using jndi to access the datasource. |
public void setDataSourceName(String v) {
assertInitializationAllowed();
if (cpds != null) {
throw new IllegalStateException(
"Cannot set the JNDI name for the DataSource, if already " +
"set using setConnectionPoolDataSource.");
}
if (dataSourceName != null)
{
throw new IllegalStateException(
"The DataSourceName has already been set. " +
"It cannot be altered.");
}
this.dataSourceName = v;
instanceKey = InstanceKeyObjectFactory.registerNewInstance(this);
}
Set the name of the ConnectionPoolDataSource which backs this pool.
This name is used to look up the datasource from a jndi service
provider. |
public void setDefaultAutoCommit(boolean v) {
assertInitializationAllowed();
this.defaultAutoCommit = v;
}
Set the value of defaultAutoCommit, which defines the state of
connections handed out from this pool. The value can be changed
on the Connection using Connection.setAutoCommit(boolean).
The default is true. |
public void setDefaultReadOnly(boolean v) {
assertInitializationAllowed();
this.defaultReadOnly = v;
}
Set the value of defaultReadOnly, which defines the state of
connections handed out from this pool. The value can be changed
on the Connection using Connection.setReadOnly(boolean).
The default is false. |
public void setDefaultTransactionIsolation(int v) {
assertInitializationAllowed();
switch (v) {
case Connection.TRANSACTION_NONE:
case Connection.TRANSACTION_READ_COMMITTED:
case Connection.TRANSACTION_READ_UNCOMMITTED:
case Connection.TRANSACTION_REPEATABLE_READ:
case Connection.TRANSACTION_SERIALIZABLE:
break;
default:
throw new IllegalArgumentException(BAD_TRANSACTION_ISOLATION);
}
this.defaultTransactionIsolation = v;
}
Set the value of defaultTransactionIsolation, which defines the state of
connections handed out from this pool. The value can be changed
on the Connection using Connection.setTransactionIsolation(int).
The default is JDBC driver dependent. |
public void setDescription(String v) {
this.description = v;
}
Set the description. This property is defined by jdbc as for use with
GUI (or other) tools that might deploy the datasource. It serves no
internal purpose. |
public void setJndiEnvironment(String key,
String value) {
if (jndiEnvironment == null) {
jndiEnvironment = new Properties();
}
jndiEnvironment.setProperty(key, value);
}
Sets the value of the given JNDI environment property to be used when
instantiating a JNDI InitialContext. This InitialContext is used to
locate the backend ConnectionPoolDataSource. |
public void setLogWriter(PrintWriter v) {
this.logWriter = v;
}
Set the value of logWriter. |
public void setLoginTimeout(int v) {
this.loginTimeout = v;
}
Set the value of loginTimeout. |
public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
assertInitializationAllowed();
_minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
Sets the minimum amount of time an object may sit idle in the pool
before it is eligable for eviction by the idle object evictor
(if any).
When non-positive, no objects will be evicted from the pool
due to idle time alone. |
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
assertInitializationAllowed();
_numTestsPerEvictionRun = numTestsPerEvictionRun;
}
Sets the number of objects to examine during each run of the
idle object evictor thread (if any).
When a negative value is supplied, ceil({*link #numIdle})/abs({*link #getNumTestsPerEvictionRun})
tests will be run. I.e., when the value is -n, roughly one nth of the
idle objects will be tested per run. |
public void setRollbackAfterValidation(boolean rollbackAfterValidation) {
assertInitializationAllowed();
this.rollbackAfterValidation = 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. Default behavior is NOT
to issue a rollback. The setting will only have an effect
if a validation query is set |
public void setTestOnBorrow(boolean testOnBorrow) {
assertInitializationAllowed();
_testOnBorrow = testOnBorrow;
testPositionSet = true;
}
When true, objects will be
{*link PoolableObjectFactory#validateObject validated}
before being returned by the {*link #borrowObject}
method. If the object fails to validate,
it will be dropped from the pool, and we will attempt
to borrow another. For a true value to have any effect,
the validationQuery property must be set to a non-null
string. |
public void setTestOnReturn(boolean testOnReturn) {
assertInitializationAllowed();
_testOnReturn = testOnReturn;
testPositionSet = true;
}
When true, objects will be
{*link PoolableObjectFactory#validateObject validated}
before being returned to the pool within the
{*link #returnObject}. For a true value to have any effect,
the validationQuery property must be set to a non-null
string. |
public void setTestWhileIdle(boolean testWhileIdle) {
assertInitializationAllowed();
_testWhileIdle = testWhileIdle;
testPositionSet = true;
}
When true, objects will be
{*link PoolableObjectFactory#validateObject validated}
by the idle object evictor (if any). If an object
fails to validate, it will be dropped from the pool. For a
true value to have any effect,
the validationQuery property must be set to a non-null
string. |
public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
assertInitializationAllowed();
_timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
Sets the number of milliseconds to sleep between runs of the
idle object evictor thread.
When non-positive, no idle object evictor thread will be
run. |
public void setValidationQuery(String validationQuery) {
assertInitializationAllowed();
this.validationQuery = validationQuery;
if (!testPositionSet) {
setTestOnBorrow(true);
}
}
The SQL query that will be used to validate connections from this pool
before returning them to the caller. If specified, this query
MUST be an SQL SELECT statement that returns at least
one row. Default behavior is to test the connection when it is
borrowed. |
abstract protected void setupDefaults(Connection con,
String username) throws SQLException
|
protected ConnectionPoolDataSource testCPDS(String username,
String password) throws SQLException, NamingException {
// The source of physical db connections
ConnectionPoolDataSource cpds = this.cpds;
if (cpds == null) {
Context ctx = null;
if (jndiEnvironment == null) {
ctx = new InitialContext();
} else {
ctx = new InitialContext(jndiEnvironment);
}
Object ds = ctx.lookup(dataSourceName);
if (ds instanceof ConnectionPoolDataSource) {
cpds = (ConnectionPoolDataSource) ds;
} else {
throw new SQLException("Illegal configuration: "
+ "DataSource " + dataSourceName
+ " (" + ds.getClass().getName() + ")"
+ " doesn't implement javax.sql.ConnectionPoolDataSource");
}
}
// try to get a connection with the supplied username/password
PooledConnection conn = null;
try {
if (username != null) {
conn = cpds.getPooledConnection(username, password);
}
else {
conn = cpds.getPooledConnection();
}
if (conn == null) {
throw new SQLException(
"Cannot connect using the supplied username/password");
}
}
finally {
if (conn != null) {
try {
conn.close();
}
catch (SQLException e) {
// at least we could connect
}
}
}
return cpds;
}
|
protected byte whenExhaustedAction(int maxActive,
int maxWait) {
byte whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
if (maxActive < = 0) {
whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_GROW;
} else if (maxWait == 0) {
whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
}
return whenExhausted;
}
|