![](/img/trans.png)
[英]C3P0 connection pooling with hibernate doesnt work with cloudfoundry
[英]Configure Hibernate C3P0 Connection Pooling
我在使用Spring和Hibernate开发基于Struts2的Web应用程序时偶然发现了一个问题。
当我刷新网站几次,例如2-4次时,Hibernate出现了一个关于太多连接的例外。 我试图实现C3P0连接池,并遇到一些问题
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中
<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>
日志输出是:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
和:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
这就是PROCESSLIST MySQL窗口的外观: http : //img844.imageshack.us/img844/3959/be69273cc2.png
我已经将连接的max_size设置为20,但它似乎没有从文件中读取C3P0配置,因为从屏幕我们可以看到连接数高于20,或者我可能做错了什么,但是哪里? 我真的需要一些帮助,我会很感激,并提前感谢。
在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>
请参阅此链接以获得更好的理解: 配置属性
在您的applicationContext中,而不是使用<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" ...
尝试使用<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ...
C3P0PooledDataSource将使用指定的DriverClassName,url,用户名和密码创建一个包装的数据库连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.