java - 最后一个数据包成功发送到服务器是在 > 70,400,003 毫秒之前。比服务器配置的长

标签 java mysql hibernate jpa c3p0

我有以下异常

javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 70,400,002 milliseconds ago. The last packet sent successfully to the server was 70,400,003 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

我做了一些研究并将 persistance.xml 更改为此

最新

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="unicorn" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.rh.xxx</class>
    <properties>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://xxx:3306/unicorndb?zeroDateTimeBehavior=convertToNull"/>
      <property name="hibernate.connection.password" value="xxx"/>
      <property name="hibernate.connection.username" value="student"/>
      <property name="hibernate.c3p0.max_size" value="100" />  <!--max number of JDBC connections -->
      <property name="hibernate.c3p0.min_size" value="10" />   <!--minimum number of JDBC connections-->
      <property name="hibernate.c3p0.idle_test_period" value="500" />
      <property name="hibernate.c3p0.acquire_increment" value="1" />
    </properties>
  </persistence-unit>
</persistence>

最新代码看起来正确吗?

如有任何帮助,我们将不胜感激

最佳答案

首先,保持测试简单,只需使用

<property name="hibernate.c3p0.testConnectionOnCheckout" value="true" />

而不是空闲连接检查。请参阅 Connection testing 上的 c3p0 文档

如果问题仍然存在,那么问题可能不是池所持有的连接,而是您的应用程序正在 checkout 并无限期保持打开状态的某些连接。理想情况下,连接应该被 checkout 、使用,然后立即 checkin (并且稳健地使用 try-with-resources 或仔细的 finally block )。

关于java - 最后一个数据包成功发送到服务器是在 > 70,400,003 毫秒之前。比服务器配置的长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47011116/

相关文章:

java - 使用运行时调用的抽象方法的值

java - 使用 Javassist 向运行时生成的方法/类添加注释

java - 表单未将数据发布到 Spring Controller

php - 插入包含撇号(单引号)的数据时出现 MySQL 错误?

php mysql获取最后插入行的ID给出错误

php - 在 Select HTML 中显示 MySQL 字段的所有记录

java - Hibernate.initialize() 似乎不适用于延迟加载的集合

java - ComboBox ChangeListener 在 AutoCompleteDropDown 中被调用 3 次

mysql - 如何设置由单独表上的多个外键引用的 Hibernate 实体?

java - 用于索引和搜索的 Lucene 分析器