Spring Connection Pooling with DBCP

Recently I wanted to add a connection pool to my Spring Web Application. I decided to use Commons DBCP to provide the connection pool as I’m using the Spring JDBC wrapper classes. Googling around didn’t find any examples of how to set up DBCP, so I’ve written my findings here.

Adding a connection pool to a Spring app is simply a matter of specifying the relevant entries in the Spring servlet configuration file. The XML snippet below shows an example of how a database connection pool can easily be configured.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"> 
  <property name="driverClassName"> 
    <value>net.sourceforge.jtds.jdbc.Driver</value> 
  </property> 

  <property name="url"> 
    <value>jdbc:jtds:sqlserver://localhost:1433/db;prepareSQL=0; 
SendStringParametersAsUnicode=False;</value> 
  </property> 
  <property name="username"> 
    <value>username</value> 

  </property> 
  <property name="password"> 
    <value>password</value> 
  </property> 
  <property name="initialSize"> 

    <value>2</value> 
  </property> 
  <property name="maxActive"> 
    <value>5</value> 
  </property> 

  <property name="maxIdle"> 
    <value>2</value> 
  </property> 
</bean>