Chapter 6. DBCP (Database Connection Pool)


Opening a connection to a database is a relatively expensive operation. To avoid having to frequently open database connections, many applications rely on a database connection poola group of connections to the database, rotated between the various components of an application as needed. The Apache Jakarta Commons Database Connection Pool (DBCP) provides a rich set of functionality for connection pooling.

Note:

Most modern application servers include built-in database connection pooling functionality. If you are building an application that you intend to host in an application server, consult your server documentation for more information on the preferred connection pool mechanism.


DBCP offers a variety of mechanisms for managing your database connection pool. As shown in Figure 6-1, the pool library mimics many aspects of the normal JDBC interfaces. The best option for your application will likely depend on the environment and your existing application code (if any).

Figure 6-1. DBCP and JDBC interfaces.


If you have an existing application that accesses a database directly (for example, a Swing application that opens JDBC connections directly to a database[1]), you may wish to take advantage of DBCP's PoolingDriver classyou can register a pool as a driver, and as long as your application code can easily be converted to using a new set of JDBC connection parameters, you can quickly add database connection pooling. Depending on the classes and configuration chosen, a wide suite of possible options is available.

[1] Perhaps even a bundled in-process Java database, such as hsqldb (http://hsqldb.sourceforge.net/) or Apache Derby (http://incubator.apache.org/derby/).

Note:

While the Apache DBCP code includes reference to logging, this functionality is deprecated (and the current implementation lacks certain key features). Future releases of Apache Commons DBCP may include pluggable mechanisms (for example, event listeners) for more sophisticated notification of pool behavior.


Most web applications, however, rely on a javax.sql.DataSource for their database connectivity, typically obtained via a JNDI lookup. The DataSource configuration in this case is managed by the application server configuration. For example, Tomcat allows you to configure JDBC connection pools as shown at http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-resources-howto.html. If you are using Tomcat as the provider of your DataSource objects, you're using the Commons DBCP package, even though you may never need to know (or care) about the underlying implementation.



    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net