Thursday, March 24, 2011

Implementing connection pool in java

DBCP (Database Connection Pool API)
Many Apache projects support interaction with a relational database. Creating a new connection for each user can be time consuming (often requiring multiple seconds of clock time), in order to perform a database transaction that might take milliseconds. Opening a connection per user can be unfeasible in a publicly-hosted Internet application where the number of simultaneous users can be very large. Accordingly, developers often wish to share a "pool" of open connections between all of the application's current users. The number of users actually performing a request at any given time is usually a very small percentage of the total number of active users, and during request processing is the only time that a database connection is required. The application itself logs into the DBMS, and handles any user account issues internally.

There are several Database Connection Pools already available, both within Apache products and elsewhere. This Commons package provides an opportunity to coordinate the efforts required to create and maintain an efficient, feature-rich package under the ASF license.

Spring bean definition of DBCP -
    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${JDBC_DRIVER_CLASSNAME}" />
        <property name="url" value="${JDBC_URL}" />
        <property name="username" value="${JDBC_USERNAME}"/>
        <property name="password" value="${JDBC_PASSWORD}" />
        <property name="initialSize" value="20" />
        <property name="maxIdle" value="30" />
        <property name="maxActive" value="50" />
        <property name="maxWait" value="120000" />
     <property name="removeAbandonedTimeout" value="10" />
        <property name="removeAbandoned" value="true" />
    </bean>

Maven Dependency
 <dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
 </dependency>

http://commons.apache.org/dbcp/configuration.html

No comments:

Post a Comment