| org.apache.commons.dbcp.cpdsadapter | This package contains one public class which is a ConnectionPoolDataSource (CPDS) implementation that can be used to adapt older Driver based jdbc implementations. |
| org.apache.commons.dbcp.datasources | This package contains two DataSources: PerUserPoolDataSource and SharedPoolDataSource which provide a database connection pool. |
| ConnectionFactory | Abstract factory interface for creating java.sql.Connection s. | code | html |
| TestConnectionPool | Base test suite for DBCP pools. | code | html |
| AbandonedConfig | Configuration settings for handling abandoned db connections. | code | html |
| AbandonedObjectPool | An implementation of a Jakarta-Commons ObjectPool which tracks JDBC connections and can recover abandoned db connections. |
code | html |
| AbandonedTrace | Tracks db connection usage for recovering and reporting abandoned db connections. | code | html |
| BasicDataSource | Basic implementation of |
code | html |
| BasicDataSourceFactory | JNDI object factory that creates an instance of
|
code | html |
| DataSourceConnectionFactory | A DataSource -based implementation of ConnectionFactory . | code | html |
| DbcpException | Subclass of |
code | html |
| DelegatingCallableStatement | A base delegating implementation of CallableStatement . | code | html |
| DelegatingConnection | A base delegating implementation of Connection . | code | html |
| DelegatingPreparedStatement | A base delegating implementation of PreparedStatement . | code | html |
| DelegatingResultSet | A base delegating implementation of ResultSet . | code | html |
| DelegatingStatement | A base delegating implementation of Statement . | code | html |
| DriverConnectionFactory | A Driver -based implementation of ConnectionFactory . | code | html |
| DriverManagerConnectionFactory | A DriverManager -based implementation of ConnectionFactory . | code | html |
| PoolableConnection | A delegating connection that, rather than closing the underlying connection, returns itself to an ObjectPool when closed. | code | html |
| PoolableConnectionFactory | A PoolableObjectFactory that creates PoolableConnection s. | code | html |
| PoolablePreparedStatement | A DelegatingPreparedStatement that cooperates with PoolingConnection to implement a pool of PreparedStatement s. | code | html |
| PooledTestObject | code | html | |
| PoolingConnection | A DelegatingConnection that pools PreparedStatement s. | code | html |
| PoolingConnection.PStmtKey | A key uniquely identifiying PreparedStatement s. | code | html |
| PoolingDataSource | A simple DataSource implementation that obtains Connection s from the specified ObjectPool . | code | html |
| PoolingDataSource.PoolGuardConnectionWrapper | PoolGuardConnectionWrapper is a Connection wrapper that makes sure a closed connection cannot be used anymore. | code | html |
| PoolingDriver | A Driver implementation that obtains Connection s from a registered ObjectPool . | code | html |
| PoolingDriver.PoolGuardConnectionWrapper | PoolGuardConnectionWrapper is a Connection wrapper that makes sure a closed connection cannot be used anymore. | code | html |
| SQLNestedException | A SQLException subclass containing another Throwable | code | html |
| TestAbandonedObjectPool.ConcurrentBorrower | code | html | |
| TestAbandonedObjectPool.SimpleFactory | code | html | |
| TestConnectionPool.TestThread | code | html | |
| TesterConnection | A dummy Connection , for testing purposes. | code | html |
| TesterDriver | Mock object implementing the java.sql.Driver interface. |
code | html |
| TesterPreparedStatement | A dummy PreparedStatement , for testing purposes. | code | html |
| TesterResultSet | A dummy ResultSet , for testing purposes. | code | html |
| TesterStatement | A dummy Statement , for testing purposes. | code | html |
| TestConnectionPool | Base test suite for DBCP pools. | code | html |
| TestAbandonedBasicDataSource | TestSuite for BasicDataSource with abandoned connection trace enabled | code | html |
| TestAbandonedObjectPool | TestCase for AbandonedObjectPool | code | html |
| TestAll | code | html | |
| TestBasicDataSource | TestSuite for BasicDataSource | code | html |
| TestBasicDataSourceFactory | TestSuite for BasicDataSourceFactory | code | html |
| TestDelegatingConnection | code | html | |
| TestDelegatingPreparedStatement | code | html | |
| TestDelegatingStatement | code | html | |
| TestJOCLed | code | html | |
| TestManual | Tests for a "manually configured", GenericObjectPool based PoolingDriver . | code | html |
| TestOracleBasicDataSource | TestSuite for BasicDataSource | code | html |
| TestPStmtPoolingBasicDataSource | TestSuite for BasicDataSource with prepared statement pooling enabled | code | html |
Database Connection Pool API.
Overview in Dialog FormQ: How do I use the DBCP package?
A: There are two primary ways to access the DBCP pool, as a Driver , or as a DataSource . You'll want to create an instance of org.apache.commons.dbcp.PoolingDriver or org.apache.commons.dbcp.PoolingDataSource . When using one of these interfaces, you can just use your JDBC objects the way you normally would. Closing a java.sql.Connection will simply return it to its pool.
Q: But PoolingDriver and PoolingDataSource both expect an ObjectPool as an input. Where do I get one of those?
A: The ObjectPool interface is defined in the org.apache.commons.pool package (Commons-Pool). The org.apache.commons.pool.impl package has a couple of implementations, and you can always create your own.
Q: Ok, I've found an ObjectPool implementation that I think suits my connection pooling needs. But it wants a PoolableObjectFactory . What should I use for that?
A: The DBCP package provides a class for this purpose. It's called org.apache.commons.dbcp.PoolableConnectionFactory . It implements the factory and lifecycle methods of org.apache.commons.pool.PoolableObjectFactory for java.sql.Connection s. But it doesn't create the actual database java.sql.Connection s itself, if uses a org.apache.commons.dbcp.ConnectionFactory for that. The org.apache.commons.dbcp.PoolableConnectionFactory will take java.sql.Connection s created by the org.apache.commons.dbcp.ConnectionFactory and wrap them with classes that implement the pooling behaviour.
Several implementations of org.apache.commons.dbcp.ConnectionFactory are provided--one that uses java.sql.DriverManager to create connections (org.apache.commons.dbcp.DriverManagerConnectionFactory ), one that uses a java.sql.Driver to create connections (org.apache.commons.dbcp.DriverConnectionFactory ), one that uses a javax.sql.DataSource to create connections (org.apache.commons.dbcp.DataSourceConnectionFactory ).
Q: I think I'm starting to get it, but can you walk me though it again?
A: Sure. Let's assume you want to create a javax.sql.DataSource that pools java.sql.Connection s. Let's also assume that that those pooled java.sql.Connection s should be obtained from the java.sql.DriverManager . You'll want to create a org.apache.commons.dbcp.PoolingDataSource .
The org.apache.commons.dbcp.PoolingDataSource uses an underlying org.apache.commons.pool.ObjectPool to create and store its java.sql.Connection .
To create a org.apache.commons.pool.ObjectPool , you'll need a org.apache.commons.pool.PoolableObjectFactory that creates the actual java.sql.Connection s. That's what org.apache.commons.dbcp.PoolableConnectionFactory is for.
To create the org.apache.commons.dbcp.PoolableConnectionFactory , you'll need at least two things:
In code, that might look like this:
GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:some:connect:string", "username", "password");
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
To create a org.apache.commons.dbcp.PoolingDriver , we do the same thing, except that instead of creating a javax.sql.DataSource on the last line, we create a org.apache.commons.dbcp.PoolingDriver , and register the connectionPool with it. E.g.,:
GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:some:connect:string", "username", "password");
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
PoolingDriver driver = new PoolingDriver();
driver.registerPool("example",connectionPool);
Since the org.apache.commons.dbcp.PoolingDriver registers itself with the java.sql.DriverManager when it is created, now you can just go to the java.sql.DriverManager to create your java.sql.Connection s, like you normally would:
Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
Q: Sounds complicated, is there an easier way?
A: If you're using the org.apache.commons.dbcp.PoolingDriver , you don't need to do this configuration in code. Instead, you can provide a JOCL document that describes the connection pool, and let the org.apache.commons.dbcp.PoolingDriver discover it at runtime.
Specifically, if the org.apache.commons.dbcp.PoolingDriver is asked for a java.sql.Connection from a pool that has not yet been registered, it will look for a named resource from which to read the pool's configuration, and create that pool.
For example, suppose you create a pool named "/eg" from a JOCL document. The "connect string" for this pool will be "jdbc:apache:commons:dbcp:/eg". To do this, you'll need a create a resource (just a file in your classpath) containing a JOCL description of the pool. Specifically, this JOCL document should define a org.apache.commons.dbcp.PoolableConnectionFactory from which the pool will be obtained. For example:
<object class="org.apache.commons.dbcp.PoolableConnectionFactory" xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl">
<!-- the first argument is the ConnectionFactory -->
<object class="org.apache.commons.dbcp.DriverManagerConnectionFactory">
<string value="jdbc:some:connect:string"/>
<object class="java.util.Properties" null="true"/>
</object>
<!-- the next argument is the ObjectPool -->
<object class="org.apache.commons.pool.impl.GenericObjectPool">
<object class="org.apache.commons.pool.PoolableObjectFactory" null="true"/>
<int value="10"/> <!-- max active -->
<byte value="1"/> <!-- when exhausted action, 0 = fail, 1 = block, 2 = grow -->
<long value="2000"/> <!-- max wait -->
<int value="10"/> <!-- max idle -->
<boolean value="false"/> <!-- test on borrow -->
<boolean value="false"/> <!-- test on return -->
<long value="10000"/> <!-- time between eviction runs -->
<int value="5"/> <!-- number of connections to test per eviction run -->
<long value="5000"/> <!-- min evictable idle time -->
<boolean value="true"/> <!-- test while idle -->
</object>
<!-- the next argument is the KeyedObjectPoolFactory -->
<object class="org.apache.commons.pool.impl.StackKeyedObjectPoolFactory">
<int value="5"/> <!-- max idle -->
</object>
<string value="SELECT COUNT(*) FROM DUAL"/> <!-- validation query -->
<boolean value="false"/> <!-- default read only -->
<boolean value="true"/> <!-- default auto commit -->
</object>
Simply save that file somewhere in your classpath as eg.jocl, and the org.apache.commons.dbcp.PoolingDriver will find it automatically. You need only register the org.apache.commons.dbcp.PoolingDriver (for example, using the jdbc.drivers property), and use the the java.sql.DriverManager to create your java.sql.Connection s, like you normally would:
Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:/eg");
(Note that without the leading slash, the pool must be located at org/apache/commons/dbcp/PoolingDriver/eg.jocl within your classpath. See java.lang.Class#getResource for details.)