Hibernate DB 连接池 c3p0 出现问题

发布于 2024-09-25 14:41:35 字数 2676 浏览 2 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花心好男孩 2024-10-02 14:41:35

对于我来说,你的 c3p0 配置不正确。

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

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

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

另请参阅 c3p0 的官方文档 在这里

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

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.

攀登最高峰 2024-10-02 14:41:35

看来与数据库的连接已超时并已被数据库服务器终止。您应该增加服务器可以等待的时间< /a> 或将 ?autoReconnect=true 附加到您的 jdbc 连接字符串。

It seems that the connection to the database has timed out and has been terminated by the database server. You should either increase the time the server can wait or append ?autoReconnect=true to your jdbc connection string.

风吹雪碎 2024-10-02 14:41:35

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

问题可能与此相关:

  • 您打开一个休眠会话
  • 执行一些比 wait_timeout 您的数据库
  • 您尝试使用此休眠会话。您在此处评论的消息将出现

我已在 此处记录了一个案例

I think this post might help. If it is the case, increasing the value of wait_timeout only postpones the ocurrence more time, rather than solving the problem.

The problem could be related with this:

  • You open an hibernate session
  • Do some things that take more time than the value of wait_timeout of your database
  • You try to use this hibernate session. The message that you comment here will appear

I've documented a case in here if you are interested.

橙幽之幻 2024-10-02 14:41:35

在根目录中的 c3po.properties 文件中输入以下行 (1)。(您应该使用 hibenate-c3po 3.6.10 Final.jar)并在 Hibeanate.cfg.xml 中使用添加 (2) 部分。

  1. c3p0.testConnectionOnCheckout=true

  2. 100<属性名称=“hibernate.c3p0.max_size”>30
    <属性名称=“hibernate.c3p0.max_statements”>10
    <属性名称=“hibernate.c3p0.min_size”>10
    <属性名称=“hibernate.c3p0.timeout”>1800
    <属性名称=“hibernate.c3p0.validate”>true
    <属性>

Enter the below line (1) in c3po.properties file in your root derectory.(you should used hibenate-c3po 3.6.10 final.jar) And in the Hibeanate.cfg.xml used add (2) part .

  1. c3p0.testConnectionOnCheckout=true

  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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文