spring 配置 Hibernate C3P0 连接池

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17062486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:00:44  来源:igfitidea点击:

Configure Hibernate C3P0 Connection Pooling

springhibernatestruts2connection-poolingc3p0

提问by Denees

I'm stumbled upon a problem while developing a Web Application based on Struts2 with Spring and Hibernate.

在使用 Spring 和 Hibernate 开发基于 Struts2 的 Web 应用程序时,我偶然发现了一个问题。

When I refresh the site a few times, for example 2-4 times, Hibernate is showing up an exception about Too many connections. I've tried to implement C3P0 Connection pool and have some problems with it

当我刷新站点几次(例如 2-4 次)时,Hibernate 会显示有关Too many connections的异常。我已经尝试实现 C3P0 连接池并且有一些问题



The hibernate.cfg.xmlconfiguration:

hibernate.cfg.xml的配置:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/axelle</property>
<property name="hibernate.connection.username">axelle</property>
<property name="hibernate.connection.password">dbpassword</property>
<property name="hibernate.current_session_context_class">thread</property>    

<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>

applicationContext.xml

应用上下文.xml

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="classpath:jdbc.properties"/>

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}"
      p:connectionProperties="${jdbc.connectionProperties}"/>

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref local="sessionFactory"/>
    </property>
</bean>

The log output is:

日志输出是:

org.hibernate.exception.JDBCConnectionException: Cannot open connection

and:

和:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"

And this is how PROCESSLIST MySQL window looks: http://img844.imageshack.us/img844/3959/be69273cc2.png

这就是 PROCESSLIST MySQL 窗口的外观:http: //img844.imageshack.us/img844/3959/be69273cc2.png



I've set max_sizeof connections to 20, but it seems like it doesn't read the C3P0 configuration from file, cause from the screen we can see that number of connections is higher than 20, or maybe I'm doing something wrong, but where? I really need some help guys, I'll appreciate this, and thanks in advance.

我已经将连接的max_size设置为 20,但它似乎没有从文件中读取 C3P0 配置,因为从屏幕上我们可以看到连接数大于 20,或者我做错了什么,但是哪里?我真的需要一些帮助,我会很感激的,并提前致谢。

回答by UdayKiran Pulipati

Mention these property in your hibernate.cfg.xml file

在 hibernate.cfg.xml 文件中提及这些属性

    <property name="hibernate.c3p0.acquire_increment">1</property>
    <property name="hibernate.c3p0.idle_test_period">100</property>
    <property name="hibernate.c3p0.max_size">10</property>
    <property name="hibernate.c3p0.max_statements">10</property>
    <property name="hibernate.c3p0.min_size">10</property>
    <property name="hibernate.c3p0.timeout">100</property>

Refer this link for better understanding: Configuration Properties

请参阅此链接以获得更好的理解:配置属性

回答by Kristiaan

In your applicationContext, instead of using <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" ...try using <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ...The C3P0PooledDataSource will create a wrapped database connection using the specified DriverClassName, url, username and password

在您的 applicationContext 中,不是使用<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" ...try using <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ...The C3P0PooledDataSource 将使用指定的 DriverClassName、url、用户名和密码创建一个包装的数据库连接