简体   繁体   中英

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:

<hibernate-configuration>
    <session-factory>
        <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">
            com.brs.dao.DBCPConnectionProvider
        </property>
        <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"/>
    </session-factory>
</hibernate-configuration>

I have followed the following tutorial

http://www.mkyong.com/hibernate/how-to-configure-dbcp-connection-pool-in-hibernate/ 

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

11:42:11,259 INFO  [com.brs.dao.DBCPConnectionProvider] (http--127.0.0.1-8080-1) active: 1 (max: 20)   idle: 7(max: 20)
11:42:11,263 INFO  [stdout] (http--127.0.0.1-8080-1) 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--127.0.0.1-8080-1) 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM