Apache DBCP Structure
Apache DBCP Structure
Connection
Create an instance of some PoolableObjectFactory. In this case, as we want to pool Connections, a DBCP PoolableConnectionFactory. The instance is given all it needs to create java.sql.Connections, in particular a reference to the ConnectionFactory created earlier, as well as a reference to the Pool created earlier
org.apache.commons.dbcp.AbandonedTrace (deprecated)
implements
implements
com.mysql.jdbc.Connection
implements
implements
extends
reference (alternatively, a reference to a PoolingConnection if we want the Connection to keep Statements in a Pool)
org.apache.commons.dbcp.PoolableConnection
org.apache.commons.dbcp. PoolableConnectionFactory This class encapsulates the 'raw' Connection. Its close() method makes it return to its pool instead of actually close the connection to the database.
org.apache.commons.pool. KeyedObjectPoolFactory
Application: Step 5
Call 'borrowObject()' on the Pool and cast it to a java.sql.Connection
reference _connFactory
Method: createConnection()
Application: Step 6
Borrowing an Object from the Pool may cause a call to 'makeObject()' to the underlying Factory. This will cause a call to ConnectionFactory.createConnection() implements
implements implements
Application: Step 7
createConnection() delegates the task of creating a java.sql.Connection to the DriverManager, using its _connectUri to get the correct Driver (the one for "jdbc:mysql:" URIs). It then slaps a PoolableConnection around it and returns that. org.apache.commons.dbcp. DriverManagerConnectionFactory This is the java.sql.DriverManagerbased implementation of ConnectionFactory
reference _connectUri
"jdbc:mysql:" This the java.sql.Driverbased implementation of ConnectionFactory This is the javax.sql.DataSourcebased implementation of ConnectionFactory
org.apache.commons.dbcp. DriverConnectionFactory
org.apache.commons.dbcp. DataSourceConnectionFactory
reference: _driver
ref: _source
Map of explicitly registered ObjectPoolinstances (static) jdbc:mysql://127.0.0.1/mabase Internal list of registered Driver instances. When asked for a Connection through DriverManager.getConnection(uri), the DriverManager asks the Drivers whether they are able to handle the passed URI by calling their Driver.acceptsURL(URL) methods. reference acceptsURL(URI) returns true if URI starts with "jdbc:mysql:"
java.sql.Driver
javax.sql.DataSource
Application: Step 8
We finally get a Connection from the com.mysql.jdbc.Driver implements
java.sql.Driver
implements
com.mysql.jdbc.Driver
ref:_pools
org.apache.commons.dbcp. PoolingDriver
Application: Step 4
The pool that is associated to the right substring of the connectionURI for which "jdbc:apache:commons:dbcp:" has been removed on the left is fetched. In this case, "jdbc:mysql://127.0.0.1/mabase". Actually, if no appropriate pool exists, PoolingDriver tries to instantiate a new ObjectPool using a .jocl config file for which it looks in the CLASSPATH
Application: Step 3
All requests for new Connections with a connectionURI starting with "jdbc:apache:commons:dbcp:" are delegated by the java.sql.DriverManager to the registered org.apache.commons.dbcp.PoolingDriver.
Simple (and incomplete) diagram showing the structure set up by the org.apache.commons.dbcp/org.apache.commons.pool packages
The diagram also shows: What steps must be taken to set up the structure in the first place. What happens when a new java.sql.Connection is requested Assumptions: We assume that we are using org.apache.commons.dbcp.DriverManagerConnectionFactory to create the java.sql.Connections (instead of org.apache.commons.dbcp.DataSourceConnectionFactory for example). We also assume that we use the MySQL database and the com.mysql.jdbc.Driver to connect to it.
Application: Step 1
Define your new poolingConnectionURI as "jdbc:apache:commons:dbcp:" + your non-pooling connectionURI. For example, instead of "jdbc:mysql://127.0.0.1/mabase" use "jdbc:apache:commons:dbcp:jdbc:mysql://127.0.0.1/mabase"
Application: Step 2
To obtain a new Connection to your database, just call DriverManager.getConnection(poolingConnectionURI,properties), where your poolingConnectionURI is, e.g. "jdbc:apache:commons:dbcp:jdbc:mysql://127.0.0.1/mabase"
Application: Step 9
To return the Connection to its pool, call Connection.close(). As the Connection is really a org.apache.commons.dbcp.PoolableConnection, it will just return to its pool