Class DriverAdapterCPDS
- java.lang.Object
-
- org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS
-
- All Implemented Interfaces:
java.io.Serializable
,javax.naming.Referenceable
,javax.naming.spi.ObjectFactory
,javax.sql.CommonDataSource
,javax.sql.ConnectionPoolDataSource
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 aDriverManager
implementation.ConnectionPoolDataSource
s are not used within general applications. They are used byDataSource
implementations that poolConnection
s, such asSharedPoolDataSource
. A J2EE container will normally provide some method of initializing theConnectionPoolDataSource
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 poolingDataSource
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 theConnectionPoolDataSource
with or without the use of JNDI.The DriverAdapterCPDS also provides
PreparedStatement
pooling which is not generally available in jbdc2ConnectionPoolDataSource
implementation, but is addressed within the jdbc3 specification. ThePreparedStatement
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 forJdbc2PoolDataSource
without the use of JNDI.- Version:
- $Revision: 896266 $ $Date: 2010-01-05 18:20:12 -0500 (Tue, 05 Jan 2010) $
- Author:
- John D. McNally
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DriverAdapterCPDS()
Default no-arg constructor for Serialization
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 classjava.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 ofPreparedStatement
svoid
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 ofPreparedStatement
svoid
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).
-
-
-
Method Detail
-
getPooledConnection
public javax.sql.PooledConnection getPooledConnection() throws java.sql.SQLException
Attempt to establish a database connection using the default user and password.- Specified by:
getPooledConnection
in interfacejavax.sql.ConnectionPoolDataSource
- Throws:
java.sql.SQLException
-
getPooledConnection
public javax.sql.PooledConnection getPooledConnection(java.lang.String username, java.lang.String pass) throws java.sql.SQLException
Attempt to establish a database connection.- Specified by:
getPooledConnection
in interfacejavax.sql.ConnectionPoolDataSource
- Parameters:
username
- name to be used for the connectionpass
- password to be used fur the connection- Throws:
java.sql.SQLException
-
getReference
public javax.naming.Reference getReference() throws javax.naming.NamingException
Referenceable
implementation.- Specified by:
getReference
in interfacejavax.naming.Referenceable
- Throws:
javax.naming.NamingException
-
getObjectInstance
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
implements ObjectFactory to create an instance of this class- Specified by:
getObjectInstance
in interfacejavax.naming.spi.ObjectFactory
- Throws:
java.lang.Exception
-
getConnectionProperties
public java.util.Properties getConnectionProperties()
Get the connection properties passed to the JDBC driver.- Returns:
- the JDBC connection properties used when creating connections.
- Since:
- 1.3
-
setConnectionProperties
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 usinggetUser()
,getPassword()
whengetPooledConnection()
is called, or using the actual parameters to the method call whengetPooledConnection(String, String)
is called. Calls tosetUser(String)
orsetPassword(String)
overwrite the values of these properties ifconnectionProperties
is not null.- Parameters:
props
- Connection properties to use when creating new connections.- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called- Since:
- 1.3
-
getDescription
public java.lang.String getDescription()
Get the value of description. This property is here for use by the code which will deploy this datasource. It is not used internally.- Returns:
- value of description, may be null.
- See Also:
setDescription(String)
-
setDescription
public void setDescription(java.lang.String v)
Set the value of description. This property is here for use by the code which will deploy this datasource. It is not used internally.- Parameters:
v
- Value to assign to description.
-
getPassword
public java.lang.String getPassword()
Get the value of password for the default user.- Returns:
- value of password.
-
setPassword
public void setPassword(java.lang.String v)
Set the value of password for the default user.- Parameters:
v
- Value to assign to password.- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called
-
getUrl
public java.lang.String getUrl()
Get the value of url used to locate the database for this datasource.- Returns:
- value of url.
-
setUrl
public void setUrl(java.lang.String v)
Set the value of url used to locate the database for this datasource.- Parameters:
v
- Value to assign to url.- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called
-
getUser
public java.lang.String getUser()
Get the value of default user (login or username).- Returns:
- value of user.
-
setUser
public void setUser(java.lang.String v)
Set the value of default user (login or username).- Parameters:
v
- Value to assign to user.- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called
-
getDriver
public java.lang.String getDriver()
Get the driver classname.- Returns:
- value of driver.
-
setDriver
public void setDriver(java.lang.String v) throws java.lang.ClassNotFoundException
Set the driver classname. Setting the driver classname cause the driver to be registered with the DriverManager.- Parameters:
v
- Value to assign to driver.- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been calledjava.lang.ClassNotFoundException
-
getLoginTimeout
public int getLoginTimeout()
Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. NOT USED.- Specified by:
getLoginTimeout
in interfacejavax.sql.CommonDataSource
- Specified by:
getLoginTimeout
in interfacejavax.sql.ConnectionPoolDataSource
-
getLogWriter
public java.io.PrintWriter getLogWriter()
Get the log writer for this data source. NOT USED.- Specified by:
getLogWriter
in interfacejavax.sql.CommonDataSource
- Specified by:
getLogWriter
in interfacejavax.sql.ConnectionPoolDataSource
-
setLoginTimeout
public void setLoginTimeout(int seconds)
Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. NOT USED.- Specified by:
setLoginTimeout
in interfacejavax.sql.CommonDataSource
- Specified by:
setLoginTimeout
in interfacejavax.sql.ConnectionPoolDataSource
-
setLogWriter
public void setLogWriter(java.io.PrintWriter out)
Set the log writer for this data source. NOT USED.- Specified by:
setLogWriter
in interfacejavax.sql.CommonDataSource
- Specified by:
setLogWriter
in interfacejavax.sql.ConnectionPoolDataSource
-
isPoolPreparedStatements
public boolean isPoolPreparedStatements()
Flag to toggle the pooling ofPreparedStatement
s- Returns:
- value of poolPreparedStatements.
-
setPoolPreparedStatements
public void setPoolPreparedStatements(boolean v)
Flag to toggle the pooling ofPreparedStatement
s- Parameters:
v
- true to pool statements.- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called
-
getMaxActive
public 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.
-
setMaxActive
public 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.- Parameters:
maxActive
- the maximum number of concurrent active statements allowed- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called
-
getMaxIdle
public int getMaxIdle()
The maximum number of statements that can remain idle in the pool, without extra ones being released, or negative for no limit.- Returns:
- the value of maxIdle
-
setMaxIdle
public 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.- Parameters:
maxIdle
- The maximum number of statements that can remain idle- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called
-
getTimeBetweenEvictionRunsMillis
public int getTimeBetweenEvictionRunsMillis()
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.- Returns:
- the value of the evictor thread timer
- See Also:
setTimeBetweenEvictionRunsMillis(int)
-
setTimeBetweenEvictionRunsMillis
public void setTimeBetweenEvictionRunsMillis(int 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.- Parameters:
timeBetweenEvictionRunsMillis
-- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called- See Also:
getTimeBetweenEvictionRunsMillis()
-
getNumTestsPerEvictionRun
public int getNumTestsPerEvictionRun()
Returns the number of statements to examine during each run of the idle object evictor thread (if any). *see #setNumTestsPerEvictionRun *see #setTimeBetweenEvictionRunsMillis
-
setNumTestsPerEvictionRun
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the number of statements 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.
- Parameters:
numTestsPerEvictionRun
- number of statements to examine per run- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called- See Also:
getNumTestsPerEvictionRun()
,setTimeBetweenEvictionRunsMillis(int)
-
getMinEvictableIdleTimeMillis
public 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). *see #setMinEvictableIdleTimeMillis *see #setTimeBetweenEvictionRunsMillis
-
setMinEvictableIdleTimeMillis
public 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). When non-positive, no objects will be evicted from the pool due to idle time alone.- Parameters:
minEvictableIdleTimeMillis
- minimum time to set (in ms)- Throws:
java.lang.IllegalStateException
- ifgetPooledConnection()
has been called- See Also:
getMinEvictableIdleTimeMillis()
,setTimeBetweenEvictionRunsMillis(int)
-
isAccessToUnderlyingConnectionAllowed
public boolean isAccessToUnderlyingConnectionAllowed()
Returns the value of the accessToUnderlyingConnectionAllowed property.- Returns:
- true if access to the underlying is allowed, false otherwise.
-
setAccessToUnderlyingConnectionAllowed
public void setAccessToUnderlyingConnectionAllowed(boolean allow)
Sets the value of the accessToUnderlyingConnectionAllowed property. It controls if the PoolGuard allows access to the underlying connection. (Default: false)- Parameters:
allow
- Access to the underlying connection is granted when true.
-
getMaxPreparedStatements
public int getMaxPreparedStatements()
Returns the maximun number of prepared statements.- Returns:
- maxPrepartedStatements value
- Since:
- 1.2.2
-
setMaxPreparedStatements
public void setMaxPreparedStatements(int maxPreparedStatements)
Sets the maximum number of prepared statements.- Parameters:
maxPreparedStatements
- the new maximum number of prepared statements- Since:
- 1.2.2
-
getParentLogger
public java.util.logging.Logger getParentLogger() throws java.sql.SQLFeatureNotSupportedException
- Specified by:
getParentLogger
in interfacejavax.sql.CommonDataSource
- Throws:
java.sql.SQLFeatureNotSupportedException
-
-