mysql - Spring Boot : Communications link failure after some hours of inactivity with Hibernate, JDBC 和 MySQL

标签 mysql spring hibernate jdbc spring-boot

如果我的 Spring Boot 应用程序在几个小时内(例如在夜间)处于非事件状态,我会收到此错误:

2015-05-19 09:16:32.666  WARN 20582 --- [http-nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
2015-05-19 09:16:32.668 ERROR 20582 --- [http-nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : Communications link failure

The last packet successfully received from the server was 29.792.613 milliseconds ago.  The last packet sent successfully to the server was 6 milliseconds ago.

试图解决这个问题,我读到 MySQL 有一个名为 wait_timeout 的参数,默认设置为 8 小时(28800 秒),在这段时间之后我所有的非事件连接都是关闭,所以我的 Spring Boot 应用程序停止工作......

我的问题是:

  • 我怎样才能避免这个问题?
  • 我应该增加此类参数的值吗?
  • 增加这种值(value)有什么缺点吗? (如果它长时间处于非事件状态,它可能始终是我的应用程序达到的值。:/)
  • 是否有另一种使用 Spring Boot 的解决方案(比如轮询或类似的东西)?

编辑

这里还有其他类似/有用的问题:

从上面的引用资料我可以得出结论,一种解决方案是添加 C3p0 作为附加库并正确配置它以避免通信链接错误..

这是我唯一的解决方案吗?是否有与 Spring/Spring Boot 更“集成”的解决方案(即不添加外部库)?

最佳答案

我解决了这里描述的问题: http://blog.netgloo.com/2015/07/09/spring-boot-communications-link-failure-with-mysql-and-hibernate/ ,在 application.properties 文件中添加这些配置:

spring.datasource.tomcat.testWhileIdle = true
spring.datasource.tomcat.timeBetweenEvictionRunsMillis = 60000
spring.datasource.tomcat.validationQuery = SELECT 1

关于mysql - Spring Boot : Communications link failure after some hours of inactivity with Hibernate, JDBC 和 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30336861/

相关文章:

spring - java.sql.SQLException : No suitable driver <tx:annotation-driven/>

java - JPA persistence.xml 和 MySQL - 未采用正确的 "hibernate.connection.url"目录

mysql - 即使我更改用户设置,用户 'username' @'localhost' 的访问被拒绝

java - @Retryable 在 Spring Boot 应用程序中由集成测试触发时不重试

java - 如何在 mockito 中模拟 Spring 依赖项

java - 带有自定义实体的SpringBoot注入(inject)RedisTemplate

php - 使用 PHP 在线访问独立访客

mysql - 通过 MySQL 在全文中查找单词并返回整行

PHP 将部分 MySQL 数据编码为 JSON

mysql - 陷入在 Hibernate 中编写 HQL/MySQL 查询的困境