Mark Estrada
Mark Estrada

Reputation: 9191

Setup Connection Pooling in Spring MVC

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.

Upvotes: 3

Views: 9127

Answers (3)

Ishwor
Ishwor

Reputation: 181

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.

Upvotes: 1

Jon Freedman
Jon Freedman

Reputation: 9687

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

Upvotes: 1

earldouglas
earldouglas

Reputation: 13473

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>

Upvotes: 3

Related Questions