3

How can I setup connection pooling in Spring MVC? I am working on an intranet website powered by Spring MVC 2.5 and jQuery. This is my first attempt at web development.

I am not sure but, I am only using this in my spring configuration file and I saw this in the Spring MVC step By Step tutorial

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>

 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:jdbc.properties</value>
   </list>
  </property>
 </bean>

This looks good during development and connection speed is fast but I am not sure if this will still holds true if many users are concurrently connected.

How can I achieve this? I have read that this is not an optimal connection datasource.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Estrada
  • 9,013
  • 37
  • 119
  • 186

3 Answers3

3

You might want to look at c3p0, which has some powerful configuration and optimization available.

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="..." />
    <property name="jdbcUrl" value="..." />
    <property name="user" value="..." />
    <property name="password" value="..." />
</bean>
earldouglas
  • 13,265
  • 5
  • 41
  • 50
  • As a new Spring Developer, I havent heard about this C3PO thing. But as I googled the net, I found some interesting discussion about this. One question though to clear my thoughts, If I create a bean like the one you have given above and then add the required C3PO jar file in my build path, can I say say that I am doing connection pooling already? Thanks – Mark Estrada Aug 27 '10 at 02:38
  • "If I create a bean like the one you have given above and then add the required C3PO jar file in my build path, can I say say that I am doing connection pooling already?" Yes you can, though you could say the same with BasicDataSource (it uses commons-pool). – earldouglas Aug 27 '10 at 16:55
1
For connection Pooling

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />

//Add this two more parameters
   <property name="**initialSize**" value="20" />
   <property name="**maxActive**" value="30" />


 </bean>

connection pool will create 20 database connection as initialSize is 20 and goes up to 30 Database connection if required as maxActive is 30.
Ishwor
  • 181
  • 2
  • 10
1

Your current setup is correct, all you need to do in order to use basic connection pooling is use a DataSource implementation provided by a connection pooling library, in your case Apache DBCP. See this post for a few links to other alternatives, C3P0 being one of them.

Note that when you actually use the DataSource bean you're injecting wrap it in a SimpleJdbcTemplate or use DataSourceUtils to obtain a Connection - see Spring JDBC Documentation

Community
  • 1
  • 1
Jon Freedman
  • 9,469
  • 4
  • 39
  • 58