Spring + Hibernate + DBCP configuration issue

I have a simple application for demonstrating the Spring and Hibernate integration. howtodoinjava – link.

I have the commons-dbcp and commons-pool dependencies in my pom.xml, both of version 1.4

The following is my hibernate configuration:

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.provider_class">
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">****</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="hibernate.dbcp.initialSize">8</property>
        <property name="hibernate.dbcp.maxActive">20</property>
        <property name="hibernate.dbcp.maxIdle">20</property>
        <property name="hibernate.dbcp.minIdle">0</property>
        <property name="hibernate.dbcp.validationQuery">SELECT 1</property>

        <mapping class="com.brs.entity.EmployeeEntity"/>

I have followed the following tutorial


to configure the pooling. When I run the application I get the following logs

11:42:11,259 INFO  [com.brs.dao.DBCPConnectionProvider] (http-- active: 1 (max: 20)   idle: 7(max: 20)
11:42:11,263 INFO  [stdout] (http-- Hibernate: select employeeen0_.f_id as f1_2_, employeeen0_.f_email as f2_2_, employeeen0_.f_first_name as f3_2_, employeeen0_.f_last_name as f4_2_, employeeen0_.f_telephone as f5_2_ from t_employee employeeen0_
11:42:11,273 INFO  [com.brs.dao.DBCPConnectionProvider] (http-- active: 0 (max: 20)   idle: 8(max: 20)

This is the expected behaviour as the number of idle connections are 8. But, when I use the MySql Workbench, show processlist; I see as many as 18 connections(2 default + 16 for the application).

The application simply saves an object in the DB. That’s all. Then why Workbench is showing 16 connections and the log of the application showing that there are only 8 connections idle? When I stop the application server the number of connections to the same schema in the workbench is 2(default). What does all this mean? Can some one explain what is happening?

Update: Sorry about the misleading package name of the connection provider. As I have copied the code from the below link my IDE has changed the package name.
The connection provider link is http://wiki.apache.org/commons/DBCP/Hibernate.


I resolved the problem of having double number of initial connections from the connection pool. The problem was related to the sample code from the site mentioned. It had wrong configuration for the Spring contexts. The same xml file was included in Application Context and MVC Context. I noticed it after wasting a lot of time.

The BasicDataSource was getting instantiated two times. So, there were double number of connections. Once again it is proven not to blindly trust tutorials that don’t have certain standard. One should follow StackOverflow and official documentation of the respective API.

Leave a Reply

Your email address will not be published. Required fields are marked *