RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Extend the JDK Classes with Jakarta Commons, Part II : Page 2

This second installment of a three-part series further explores components in Jakarta Commons and presents real world examples to demonstrate how you can use them in your projects.



The Database Connection Pool (DBCP) component can be used in applications where JDBC resources need to be pooled. Apart from JDBC connections, this provides support for pooling Statement and PreparedStatement instances as well.

The DBCP documentation available on the Jakarta Web site is comprehensive and useful. Particularly the DBCP Wiki site and sequence diagrams presented in the user guide are worth a look. The javadoc of the package org.apache.commons.dbcp contains some detailed information on the API as well. Information about configuring Tomcat to use DBCP can be found here.

The sample application in this section demonstrates the DBCP features by using the lightweight database engine Hypersonic SQL. The primary objective of the application is to store and retrieve contact details such as name and email address. It uses pooled JDBC connections and prepared statements provided by the DBCP framework. The source files can be found in the package in.co.narayanan.commons.dbcp in the src folder of the source code.

The application uses each of the following classes:

  • ContactInfo – A domain object that represents the details of a contact
  • ContactInfoRepository – A DAO class that contains methods such as createContactInfo and findByName for creating a contact record and finding a contact, respectively, from the database (This class is provided with a reference to the implementation of IDatabaseContext, from which the pooled connections and statements are retrieved.)
  • DatabaseContext – Uses DBCP to provide pooled connections and statements to the DAO class

Start the application by running the TestDBCP JUnit test case class. (Be sure to keep hsqldb.jar in the classpath while running the application.) This class initializes the DatabaseContext and passes the reference to ContactInfoRepository to store the details of a contact. Listing 7 shows the code for initializing the DBCP framework.

Listing 7. Initializing the DBCP Framework
// Load the HSQL Database Engine JDBC driver // hsqldb.jar should be in the class path try { Class.forName("org.hsqldb.jdbcDriver"); } catch (ClassNotFoundException e) { throw new DbException("Unable to load the db driver", e); } Properties db = getDBProperties(); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(db.getProperty("url"), db.getProperty("user"), db.getProperty("password")); connectionPool = new GenericObjectPool(); KeyedObjectPoolFactory stmtPool = new GenericKeyedObjectPoolFactory(null); new PoolableConnectionFactory(connectionFactory,connectionPool,stmtPool,null,false,true); dataSource = new PoolingDataSource(connectionPool);

DBCP uses the Commons Pool framework for pooling connections and statement references. The framework is generic enough to pool any object. During instantiation, the PoolableConnectionFactory class registers itself to the GenericObjectPool instance passed in its constructor. This factory class is used to create new instances of the JDBC connections.

The connection pool reference needs to be passed on to PoolingDataSource. The PoolingDataSource class implements the javax.sql.DataSource interface to provide pooled connections. The dataSource variable is available to the DAO class for fetching a connection.

Passing an instance of the KeyedObjectPoolFactory class to the PoolableConnectionFactory constructor enables pooling the prepared statement objects instances that a JDBC connection creates. The KeyedObjectPoolFactory is a variant of pool factory wherein the resources are pooled per key. For the prepared statements, the query acts as the key. Hence, a pooled prepared statement instance is returned on a call to the same query.

Listing 8 is a very common method in a DAO class. The boldface lines depict fetching a connection from the DatabaseContext and using it to insert a record to the database with a prepared statement.

Listing 8. Usage of the Pooled Connection and PreparedStatement Objects
String query = "INSERT INTO contactinfo(name,email) VALUES(?,?)"; Connection con = null; PreparedStatement createContactPrStmt = null; try { try { con = context.getConnection(); createContactPrStmt = context.getPreparedStatement(con, query); createContactPrStmt.setString(1, contactInfo.getName()); createContactPrStmt.setString(2, contactInfo.getEmailAddress()); createContactPrStmt.execute(); } finally { if(createContactPrStmt != null) { createContactPrStmt.close(); } if(con != null) { con.close(); } } } catch(SQLException e) { throw new DbException("Unable to create ContactInfo record", e); } catch(DbException e) { throw new DbException("Unable to create ContactInfo record", e); }

The pool used by DBCP is very customizable and hence it is suitable for enterprise software.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date