Configuring a connection pool in spring using DBCP

This page describes the process of setting up a connection pool using DBCP and spring framework.

This is good for small or prototypes applications that are running standalone and don’t have access to JNDI resources typically provided by a Java application server.

Include the following in your pom.xml file and regenerate the project to ensure you have dbcp in the classpath.


Inside the spring configuration file you should define the following.

        <bean id="dataSource"
                <property name="url" value="<put database connection url here>" />
                <property name="username" value="XXXXXX" />
                <property name="password" value="XXXXXXXX" />
                <property name="driverClassName" value="<database driver here>" />

        <bean id="pool" class="org.apache.commons.pool.impl.GenericObjectPool">
        <property name="minEvictableIdleTimeMillis"><value>300000</value></property>
        <property name="timeBetweenEvictionRunsMillis"><value>60000</value></property>

    <bean id="dsConnectionFactory" class="org.apache.commons.dbcp.DataSourceConnectionFactory">
        <constructor-arg><ref bean="dataSource"/></constructor-arg>

    <bean id="poolableConnectionFactory" class="org.apache.commons.dbcp.PoolableConnectionFactory">
        <constructor-arg index="0"><ref bean="dsConnectionFactory"/></constructor-arg>
        <constructor-arg index="1"><ref bean="pool"/></constructor-arg>
        <constructor-arg index="2"><null/></constructor-arg>
        <constructor-arg index="3"><null/></constructor-arg>
        <constructor-arg index="4"><value>false</value></constructor-arg>
        <constructor-arg index="5"><value>true</value></constructor-arg>

    <bean id="pooledDS" class="org.apache.commons.dbcp.PoolingDataSource" depends-on="poolableConnectionFactory">
        <constructor-arg><ref bean="pool"/></constructor-arg>

you can now inject “pooledDS” bean into any other spring enabled bean and use it just like any other dataSource.

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.

6 Responses to “Configuring a connection pool in spring using DBCP”

  1. 1 Xandrios
    May 23, 2011 at 3:52 am

    This was very helpful for my OSGi + Spring project! Thanks!

  2. 2 VTMadcow
    May 24, 2011 at 1:59 pm

    I am still a bit new to the pooling game, but with this now when I call getConnection from my datasource it will be using a pooled connection correct??!?!?!?

    This is incredibly cool!!!!!!

  3. May 24, 2011 at 7:20 pm

    Thats the idea. You can verify that by inspecting the connection object using a debugging tool. Keep in mind to clean up and close JDBC objects when done using them.

  4. August 6, 2012 at 11:51 am

    Good Stuff! works like a charm with mybatis.

  5. 5 sb
    April 22, 2013 at 4:02 pm

    we tried UCP ,C3PO , Regular Oracle .. nothing beats your solution..we are using it with Spring JDBC. Thanks a ton

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

November 2009
« Oct   Dec »

Blog Stats

  • 842,358 hits

%d bloggers like this: