Java Hibernate 与数据库的连接几分钟后超时

标签 java mysql hibernate connection

Hibernate 在几分钟后失去与数据库的连接并发送这样的错误:

org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN:

SQL Error: 0, SQLState: 08S01 paź 18, 2018 11:17:40 PM

org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR:

The last packet successfully received from the server was 363 452 milliseconds ago. The last packet sent successfully to the server was 363 493 milliseconds ago. is longer than the server configured value of 'interactive_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.

我添加了autoreconnect=true,但它不起作用,错误仍然发生。

然后我创建了DatabaseConnectionController,它具有以下功能:

public static EntityManagerFactory getEntityManagerFactory() {

    if (entityManagerFactory != null && entityManagerFactory.isOpen()) {
        return entityManagerFactory;
    } else {
        return requestNewConnection();
    }
}

问题仍然出现,我开始在google中搜索答案,我在persistance.xml中添加了几行:

<property name="hibernate.dbcp.validationQuery" value="SELECT 1" />
<property name="hibernate.dbcp.testOnBorrow" value="true" />
<property name="hibernate.dbcp.validationInterval" value="60000" />
<property name="hibernate.dbcp.testOnReturn" value="true" />

它也没有解决我的问题。

Hibernate 在第一次发送错误消息时不会自动重新连接到数据库,但是当发送第二次查询时会自动重新连接到数据库。

如何将其设置为在断开连接后自动重新连接,或者如何捕获此错误并在代码中重复查询?

最佳答案

问题是数据库将丢弃空闲连接。连接可以空闲多长时间取决于数据库的配置。为了解决这个问题,您需要使用连接池,例如 c3p0。要使用它,您需要在 Maven 中添加以下依赖项。

<!-- c3p0 -->
<!-- Session manager -->
<!-- Check that the version works for you -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.2.12.Final</version>
</dependency>

然后您需要将以下配置添加到 hibernate.cfg.xml 中。

<!-- c3p0 -->
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.timeout">600</property>
<property name="hibernate.c3p0.max_size">25</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_statement">0</property>
<property name="hibernate.c3p0.acquireRetryAttempts">1</property>
<property name="hibernate.c3p0.acquireRetryDelay">250</property>

这应该足以继续创建新连接并解决您的连接问题。

关于Java Hibernate 与数据库的连接几分钟后超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883010/

相关文章:

mysql - 为什么这个工作优先级 = '' -1 在 Mysql 中

mysql - 如何在MySQL中选择两个具有相同类别的随机行

java - Hibernate 注释错误(org.hibernate.exception.SQLGrammarException : could not insert: [com. hib.ex.entity.Node])

java - Hibernate中根据条件切换表

java - Java实现数组旋转方法

java - 创建名称为 'org.springframework.security.filterChains' Spring + Hibernate + UserDetailSservice 的 bean 时出错

java - 如何与 Hector 设置 Cassandra 中列的生存时间?

java - 您需要将 APK 文件大小减小到 100MB 或使用 APK 扩展文件

mysql - 从Mysql中的多个组中获取最新条目

mysql - 间歇性 MySQL 5.6 hibernate 错误 : Got error -1 from storage engine