Hibernate DB连接pooler c3p0有问题

时间:2010-10-01 18:59:02

标签: java mysql hibernate connection-pooling c3p0

自上一篇文章以来,所做的所有修改都有所建议,但这个问题仍然困扰着我。这是我得到的错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 44,499,102 milliseconds ago.

这是我的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>

和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

4 个答案:

答案 0 :(得分:2)

至于我,你的c3p0配置不正确。

c3p0.preferredTestQuery等属性必须位于类路径的c3p0.properties文件中(例如WEB-INF / classes)。

下面是我的适用于Oracle的c3p0.properties文件示例:

c3p0.preferredTestQuery=SELECT 1 from dual
c3p0.maxConnectionAge=3600
c3p0.testConnectionOnCheckout=true
c3p0.acquireRetryDelay=1000
c3p0.acquireRetryAttempts=30
c3p0.breakAfterAcquireFailure=false

另见c3p0 here的官方文档。

请注意您正在使用的c3p0版本。他们在早期版本的c3p0 0.9中有一个问题连接恢复。

答案 1 :(得分:0)

似乎与数据库的连接已超时并已被数据库服务器终止。你应该increase the time the server can wait或者将你的jdbc连接字符串附加?autoReconnect = true。

答案 2 :(得分:0)

我认为this post可能有所帮助。如果是这种情况,增加wait_timeout的值只会推迟发生更多的时间,而不是解决问题。

问题可能与此有关:

  • 您打开一个休眠会话
  • 做一些比数据库wait_timeout的值花费更多时间的事情
  • 您尝试使用此hibernate会话。您在此处评论的消息将显示

如果您有兴趣,我已在here中记录了一个案例。

答案 3 :(得分:0)

在根目录中的c3po.properties文件中输入以下行(1)。(您应该使用hibenate-c3po 3.6.10 final.jar)并在Hibeanate.cfg.xml中使用add(2)part。< / p>

  1. c3p0.testConnectionOnCheckout =真

  2. <property name="hibernate.c3p0.idle_test_period">100</property> <property name="hibernate.c3p0.max_size">30</property> <property name="hibernate.c3p0.max_statements">10</property> <property name="hibernate.c3p0.min_size">10</property> <property name="hibernate.c3p0.timeout">1800</property> <property name="hibernate.c3p0.validate">true</property> <property>