public class DriverAdapterCPDS
extends java.lang.Object
implements javax.sql.ConnectionPoolDataSource, javax.naming.Referenceable, java.io.Serializable, javax.naming.spi.ObjectFactory
An adapter for jdbc drivers that do not include an implementation
of ConnectionPoolDataSource
, but still include a
DriverManager
implementation.
ConnectionPoolDataSource
s are not used within general
applications. They are used by DataSource
implementations
that pool Connection
s, such as
SharedPoolDataSource
. A J2EE
container will normally provide some method of initializing the
ConnectionPoolDataSource
whose attributes are presented
as bean getters/setters and then deploying it via JNDI. It is then
available as a source of physical connections to the database, when
the pooling DataSource
needs to create a new
physical connection.
Although normally used within a JNDI environment, the DriverAdapterCPDS
can be instantiated and initialized as any bean and then attached
directly to a pooling DataSource
.
Jdbc2PoolDataSource
can use the
ConnectionPoolDataSource
with or without the use of JNDI.
The DriverAdapterCPDS also provides PreparedStatement
pooling
which is not generally available in jbdc2
ConnectionPoolDataSource
implementation, but is
addressed within the jdbc3 specification. The PreparedStatement
pool in DriverAdapterCPDS has been in the dbcp package for some time, but
it has not undergone extensive testing in the configuration used here.
It should be considered experimental and can be toggled with the
poolPreparedStatements attribute.
The package documentation contains an
example using catalina and JNDI. The datasources package documentation
shows how to use DriverAdapterCPDS
as a source for
Jdbc2PoolDataSource
without the use of JNDI.
Modifier and Type | Field and Description |
---|---|
private int |
_maxPreparedStatements |
private int |
_minEvictableIdleTimeMillis |
private int |
_numTestsPerEvictionRun |
private int |
_timeBetweenEvictionRunsMillis |
private boolean |
accessToUnderlyingConnectionAllowed
Controls access to the underlying connection
|
private java.util.Properties |
connectionProperties
Connection properties passed to JDBC Driver
|
private java.lang.String |
description
Description
|
private java.lang.String |
driver
Driver class name
|
private static java.lang.String |
GET_CONNECTION_CALLED |
private boolean |
getConnectionCalled
Whether or not getConnection has been called
|
private int |
loginTimeout
Login TimeOut in seconds
|
private java.io.PrintWriter |
logWriter
Log stream.
|
private int |
maxActive |
private int |
maxIdle |
private java.lang.String |
password
Password
|
private boolean |
poolPreparedStatements |
private static long |
serialVersionUID |
private java.lang.String |
url
Url name
|
private java.lang.String |
user
User name
|
Constructor and Description |
---|
DriverAdapterCPDS()
Default no-arg constructor for Serialization
|
Modifier and Type | Method and Description |
---|---|
private void |
assertInitializationAllowed()
Throws an IllegalStateException, if a PooledConnection has already
been requested.
|
java.util.Properties |
getConnectionProperties()
Get the connection properties passed to the JDBC driver.
|
java.lang.String |
getDescription()
Get the value of description.
|
java.lang.String |
getDriver()
Get the driver classname.
|
int |
getLoginTimeout()
Gets the maximum time in seconds that this data source can wait
while attempting to connect to a database.
|
java.io.PrintWriter |
getLogWriter()
Get the log writer for this data source.
|
int |
getMaxActive()
The maximum number of active statements that can be allocated from
this pool at the same time, or non-positive for no limit.
|
int |
getMaxIdle()
The maximum number of statements that can remain idle in the
pool, without extra ones being released, or negative for no limit.
|
int |
getMaxPreparedStatements()
Returns the maximun number of prepared statements.
|
int |
getMinEvictableIdleTimeMillis()
Returns the minimum amount of time a statement may sit idle in the pool
before it is eligible for eviction by the idle object evictor
(if any).
|
int |
getNumTestsPerEvictionRun()
Returns the number of statements to examine during each run of the
idle object evictor thread (if any).
|
java.lang.Object |
getObjectInstance(java.lang.Object refObj,
javax.naming.Name name,
javax.naming.Context context,
java.util.Hashtable env)
implements ObjectFactory to create an instance of this class
|
java.util.logging.Logger |
getParentLogger() |
java.lang.String |
getPassword()
Get the value of password for the default user.
|
javax.sql.PooledConnection |
getPooledConnection()
Attempt to establish a database connection using the default
user and password.
|
javax.sql.PooledConnection |
getPooledConnection(java.lang.String username,
java.lang.String pass)
Attempt to establish a database connection.
|
javax.naming.Reference |
getReference()
Referenceable implementation. |
int |
getTimeBetweenEvictionRunsMillis()
Returns the number of milliseconds to sleep between runs of the
idle object evictor thread.
|
java.lang.String |
getUrl()
Get the value of url used to locate the database for this datasource.
|
java.lang.String |
getUser()
Get the value of default user (login or username).
|
boolean |
isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.
|
boolean |
isPoolPreparedStatements()
Flag to toggle the pooling of
PreparedStatement s |
void |
setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property.
|
void |
setConnectionProperties(java.util.Properties props)
Set the connection properties passed to the JDBC driver.
|
void |
setDescription(java.lang.String v)
Set the value of description.
|
void |
setDriver(java.lang.String v)
Set the driver classname.
|
void |
setLoginTimeout(int seconds)
Sets the maximum time in seconds that this data source will wait
while attempting to connect to a database.
|
void |
setLogWriter(java.io.PrintWriter out)
Set the log writer for this data source.
|
void |
setMaxActive(int maxActive)
The maximum number of active statements that can be allocated from
this pool at the same time, or non-positive for no limit.
|
void |
setMaxIdle(int maxIdle)
The maximum number of statements that can remain idle in the
pool, without extra ones being released, or negative for no limit.
|
void |
setMaxPreparedStatements(int maxPreparedStatements)
Sets the maximum number of prepared statements.
|
void |
setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis)
Sets the minimum amount of time a statement may sit idle in the pool
before it is eligable for eviction by the idle object evictor
(if any).
|
void |
setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the number of statements to examine during each run of the
idle object evictor thread (if any).
|
void |
setPassword(java.lang.String v)
Set the value of password for the default user.
|
void |
setPoolPreparedStatements(boolean v)
Flag to toggle the pooling of
PreparedStatement s |
void |
setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis)
Sets the number of milliseconds to sleep between runs of the
idle object evictor thread.
|
void |
setUrl(java.lang.String v)
Set the value of url used to locate the database for this datasource.
|
void |
setUser(java.lang.String v)
Set the value of default user (login or username).
|
private static final long serialVersionUID
private static final java.lang.String GET_CONNECTION_CALLED
private java.lang.String description
private java.lang.String password
private java.lang.String url
private java.lang.String user
private java.lang.String driver
private int loginTimeout
private transient java.io.PrintWriter logWriter
private boolean poolPreparedStatements
private int maxActive
private int maxIdle
private int _timeBetweenEvictionRunsMillis
private int _numTestsPerEvictionRun
private int _minEvictableIdleTimeMillis
private int _maxPreparedStatements
private volatile boolean getConnectionCalled
private java.util.Properties connectionProperties
private boolean accessToUnderlyingConnectionAllowed
public DriverAdapterCPDS()
public javax.sql.PooledConnection getPooledConnection() throws java.sql.SQLException
getPooledConnection
in interface javax.sql.ConnectionPoolDataSource
java.sql.SQLException
public javax.sql.PooledConnection getPooledConnection(java.lang.String username, java.lang.String pass) throws java.sql.SQLException
getPooledConnection
in interface javax.sql.ConnectionPoolDataSource
username
- name to be used for the connectionpass
- password to be used fur the connectionjava.sql.SQLException
public javax.naming.Reference getReference() throws javax.naming.NamingException
Referenceable
implementation.getReference
in interface javax.naming.Referenceable
javax.naming.NamingException
public java.lang.Object getObjectInstance(java.lang.Object refObj, javax.naming.Name name, javax.naming.Context context, java.util.Hashtable env) throws java.lang.Exception
getObjectInstance
in interface javax.naming.spi.ObjectFactory
java.lang.Exception
private void assertInitializationAllowed() throws java.lang.IllegalStateException
java.lang.IllegalStateException
public java.util.Properties getConnectionProperties()
public void setConnectionProperties(java.util.Properties props)
Set the connection properties passed to the JDBC driver.
If props
contains "user" and/or "password"
properties, the corresponding instance properties are set. If these
properties are not present, they are filled in using
getUser()
, getPassword()
when getPooledConnection()
is called, or using the actual parameters to the method call when
getPooledConnection(String, String)
is called. Calls to
setUser(String)
or setPassword(String)
overwrite the values
of these properties if connectionProperties
is not null.
props
- Connection properties to use when creating new connections.java.lang.IllegalStateException
- if getPooledConnection()
has been calledpublic java.lang.String getDescription()
setDescription(String)
public void setDescription(java.lang.String v)
v
- Value to assign to description.public java.lang.String getPassword()
public void setPassword(java.lang.String v)
v
- Value to assign to password.java.lang.IllegalStateException
- if getPooledConnection()
has been calledpublic java.lang.String getUrl()
public void setUrl(java.lang.String v)
v
- Value to assign to url.java.lang.IllegalStateException
- if getPooledConnection()
has been calledpublic java.lang.String getUser()
public void setUser(java.lang.String v)
v
- Value to assign to user.java.lang.IllegalStateException
- if getPooledConnection()
has been calledpublic java.lang.String getDriver()
public void setDriver(java.lang.String v) throws java.lang.ClassNotFoundException
v
- Value to assign to driver.java.lang.IllegalStateException
- if getPooledConnection()
has been calledjava.lang.ClassNotFoundException
public int getLoginTimeout()
getLoginTimeout
in interface javax.sql.CommonDataSource
public java.io.PrintWriter getLogWriter()
getLogWriter
in interface javax.sql.CommonDataSource
public void setLoginTimeout(int seconds)
setLoginTimeout
in interface javax.sql.CommonDataSource
public void setLogWriter(java.io.PrintWriter out)
setLogWriter
in interface javax.sql.CommonDataSource
public boolean isPoolPreparedStatements()
PreparedStatement
spublic void setPoolPreparedStatements(boolean v)
PreparedStatement
sv
- true to pool statements.java.lang.IllegalStateException
- if getPooledConnection()
has been calledpublic int getMaxActive()
public void setMaxActive(int maxActive)
maxActive
- the maximum number of concurrent active statements allowedjava.lang.IllegalStateException
- if getPooledConnection()
has been calledpublic int getMaxIdle()
public void setMaxIdle(int maxIdle)
maxIdle
- The maximum number of statements that can remain idlejava.lang.IllegalStateException
- if getPooledConnection()
has been calledpublic int getTimeBetweenEvictionRunsMillis()
setTimeBetweenEvictionRunsMillis(int)
public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis)
timeBetweenEvictionRunsMillis
- java.lang.IllegalStateException
- if getPooledConnection()
has been calledgetTimeBetweenEvictionRunsMillis()
public int getNumTestsPerEvictionRun()
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
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.
numTestsPerEvictionRun
- number of statements to examine per runjava.lang.IllegalStateException
- if getPooledConnection()
has been calledgetNumTestsPerEvictionRun()
,
setTimeBetweenEvictionRunsMillis(int)
public int getMinEvictableIdleTimeMillis()
public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis)
minEvictableIdleTimeMillis
- minimum time to set (in ms)java.lang.IllegalStateException
- if getPooledConnection()
has been calledgetMinEvictableIdleTimeMillis()
,
setTimeBetweenEvictionRunsMillis(int)
public boolean isAccessToUnderlyingConnectionAllowed()
public void setAccessToUnderlyingConnectionAllowed(boolean allow)
allow
- Access to the underlying connection is granted when true.public int getMaxPreparedStatements()
public void setMaxPreparedStatements(int maxPreparedStatements)
maxPreparedStatements
- the new maximum number of prepared
statementspublic java.util.logging.Logger getParentLogger() throws java.sql.SQLFeatureNotSupportedException
getParentLogger
in interface javax.sql.CommonDataSource
java.sql.SQLFeatureNotSupportedException