Hibernate数据库连接池c3p0有问题 [英] Something wrong with Hibernate DB connection pooler c3p0
问题描述
原因:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:最后一个数据包成功接收从服务器是44499,102毫秒前。
这里是我的hibernate.cfg.xml
<?xml version ='1.0'encoding ='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate Configuration DTD // ENhttp://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd\">
< hibernate-configuration>
< session-factory>
< property name =hibernate.connection.provider_class> org.hibernate.connection.C3P0ConnectionProvider< / property>
< property name =hibernate.connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =connection.autoReconnect>真LT; /性>
< property name =connection.autoReconnectForPools> true< / property>
< property name =connection.is-connection-validation-required> true< / property>
< property name =hibernate.c3p0.acquire_increment> 5< / property>
< property name =hibernate.c3p0.max_size> 150< / property>
< property name =hibernate.c3p0.max_statements> 0< / property>
< property name =hibernate.c3p0.min_size> 10< / property>
< property name =hibernate.c3p0.timeout> 100< / property> <! - 秒 - >
< property name =hibernate.c3p0.idle_test_period> 30< / property> <! - 秒 - >
< property name =hibernate.connection.url> jdbc:mysql://!secret!autoReconnect = true< / property>
< property name =hibernate.connection.username>!secret!< / property>
< property name =hibernate.connection.password>!secret!< / property>
<! - < property name =hibernate.connection.pool_size> 10< / property> - >
< property name =show_sql> true< / property>
< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>
< property name =hibernate.hbm2ddl.auto>更新< / property>
< property name =current_session_context_class>线程< / property>
<! - 映射文件 - >
< mapping resource =mappings.hbm.xml/>
< / session-factory>
< / hibernate-configuration>
和c3p0.properties
c3p0.preferredTestQuery =从
中选择1 c3p0.maxConnectionAge = 3600
c3p0.testConnectionOnCheckin = true
c3p0.testConnectionOnCheckout = true
c3p0.acquireRetryDelay = 1000
c3p0.acquireRetryAttempts = 30
c3p0.breakAfterAcquireFailure = false
c3p0.idleConnectionTestPeriod = 100
下面是我的c3p0.properties文件的例子,它适用于Oracle: p>
c3p0.preferredTestQuery =从
中选择1 c3p0.maxConnectionAge = 3600
c3p0.testConnectionOnCheckout = true
c3p0.acquireRetryDelay = 1000
c3p0.acquireRetryAttempts = 30
c3p0.breakAfterAcquireFailure = false
另请参阅c3p0官方文档此处。
请注意您正在使用的c3p0版本。他们在c3p0 0.9的早期版本中恢复了问题连接。
since last post, did all the changes suggested but this problem still haunts me. Here's the error i get:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 44,499,102 milliseconds ago.
here's my hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.autoReconnect"> true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<property name="hibernate.c3p0.max_size">150</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">100</property> <!-- seconds -->
<property name="hibernate.c3p0.idle_test_period">30</property> <!-- seconds -->
<property name="hibernate.connection.url">jdbc:mysql://!secret!autoReconnect=true</property>
<property name="hibernate.connection.username">!secret!</property>
<property name="hibernate.connection.password">!secret!</property>
<!-- <property name="hibernate.connection.pool_size">10</property> -->
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<!-- Mapping files -->
<mapping resource="mappings.hbm.xml"/>
</session-factory>
</hibernate-configuration>
and c3p0.properties
c3p0.preferredTestQuery=select 1 from dual
c3p0.maxConnectionAge=3600
c3p0.testConnectionOnCheckin=true
c3p0.testConnectionOnCheckout=true
c3p0.acquireRetryDelay=1000
c3p0.acquireRetryAttempts=30
c3p0.breakAfterAcquireFailure=false
c3p0.idleConnectionTestPeriod=100
As for me, you're incorrectly configured c3p0.
Properties like c3p0.preferredTestQuery must be located at c3p0.properties file from your classpath (e.g. WEB-INF/classes).
Below is my example of c3p0.properties file that work nice for Oracle:
c3p0.preferredTestQuery=SELECT 1 from dual
c3p0.maxConnectionAge=3600
c3p0.testConnectionOnCheckout=true
c3p0.acquireRetryDelay=1000
c3p0.acquireRetryAttempts=30
c3p0.breakAfterAcquireFailure=false
See also official doc for c3p0 here.
And please pay your attention to version of c3p0 that you're using. They had an issue connection restoring in early releases of c3p0 0.9.
这篇关于Hibernate数据库连接池c3p0有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!