Database Connection Pool API.Overview in Dialog Form
Q: 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
PoolingDataSource. When using one
of these interfaces, you can just use your JDBC objects the way you normally would. Closing a
Connection will simply return it to its pool.
A: The DBCP package provides a class for this purpose. It's called
PoolableConnectionFactory. It implements the factory and lifecycle methods of
Connections. But it doesn't create the
Connections itself, it uses a
PoolableConnectionFactory will take
ConnectionFactory and wrap them with classes that implement the pooling
Several implementations of
ConnectionFactory are provided--one that uses
DriverManager to create connections
DriverManagerConnectionFactory), one that uses a
Driver to create
DriverConnectionFactory), one that uses a
to create connections (
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
DataSource that pools
Let's also assume that those pooled
Connections should be obtained from the
DriverManager. You'll want to create a
To create the
PoolableConnectionFactory, you'll need at least two things:
ConnectionFactoryfrom which the actual database
Connections will be obtained.
- An empty and factory-less
ObjectPoolin which the
Connections will be stored.
When you pass an
PoolableConnectionFactory, it will automatically register itself as the
PooledObjectFactoryfor that pool.
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);
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);
Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
Interface Summary Interface Description BasicDataSourceMXBeanInterface to keep API compatibility. ConnectionFactoryAbstract factory interface for creating
DataSourceMXBeanDefines the methods that will be made available via JMX. PoolableConnectionMXBeanDefines the attributes and methods that will be exposed via JMX for
Class Summary Class Description AbandonedTraceTracks connection usage for recovering and reporting abandoned connections. BasicDataSourceBasic implementation of
javax.sql.DataSourcethat is configured via JavaBeans properties.
BasicDataSourceFactoryJNDI object factory that creates an instance of
BasicDataSourcethat has been configured based on the
RefAddrvalues of the specified
Reference, which must match the names and data types of the
BasicDataSourcebean properties with the following exceptions:
connectionInitSqlsmust be passed to this factory as a single String using semicolon to delimit the statements whereas
BasicDataSourcerequires a collection of Strings.
ConstantsConstants. DataSourceConnectionFactory DelegatingCallableStatementA base delegating implementation of
DelegatingConnection<C extends Connection>A base delegating implementation of
DelegatingDatabaseMetaDataA base delegating implementation of
DelegatingPreparedStatementA base delegating implementation of
DelegatingResultSetA base delegating implementation of
DelegatingStatementA base delegating implementation of
DriverConnectionFactory DriverManagerConnectionFactory Jdbc41BridgeDefines bridge methods to JDBC 4.1 (Java 7) methods to allow call sites to operate safely (without
AbstractMethodError) when using a JDBC driver written for JDBC 4.0 (Java 6).
PoolableCallableStatement PoolableConnectionA delegating connection that, rather than closing the underlying connection, returns itself to an
PoolableConnectionFactory PoolablePreparedStatement<K> PoolingConnection PoolingDataSource<C extends Connection> PoolingDriver PStmtKeyA key uniquely identifying
SwallowedExceptionLoggerClass for logging swallowed exceptions. UtilsUtility methods.
Enum Summary Enum Description PoolingConnection.StatementTypeStatement types.
Exception Summary Exception Description ListExceptionAn exception wrapping a list of exceptions. SQLExceptionListAn SQLException based on a list of Throwable causes.