Spring 引导:使用 Hibernate、JDBC 和 MySQL 数小时不活动后,通信 link 失败
Spring Boot: Communications link failure after some hours of inactivity with Hibernate, JDBC and MySQL
如果我的 Spring 引导应用程序在几个小时内(例如在夜间)处于非活动状态,我会收到此错误消息:
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 启动应用程序停止工作...
我的问题是:
- 如何避免这个问题?
- 我应该增加这个参数的值吗?
- 增加这样的价值有什么缺点吗? (如果它长时间处于非活动状态,它可能始终是我的应用程序达到的值。:/)
- 有没有其他解决方案使用 Spring Boot(比如轮询或类似的东西)?
编辑
这里还有其他 similar/useful 个问题:
- Hibernate Communications Link Failure in Hibernate Based Java Servlet application powered by MySQL
- WARN SqlExceptionHelper:143 - SQL Error: 0, SQLState: 08S01- SqlExceptionHelper:144 - Communications link failure
- SQL Error: 0, SQLState: 08S01 Communications link failure
- https://serverfault.com/questions/89955/unable-to-connect-to-mysql-through-jdbc-connector-through-tomcat-or-externally
- Solving a "communications link failure" with JDBC and MySQL
从上面的参考资料我可以得出结论,一种解决方案是添加 C3p0 作为附加库并正确配置它以避免通信 link 错误..
这是我唯一的解决方案吗?有没有更多 "integrated" 和 Spring/Spring 启动的解决方案(即不添加外部库)?
我按照此处所述解决了问题:
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
如果我的 Spring 引导应用程序在几个小时内(例如在夜间)处于非活动状态,我会收到此错误消息:
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 启动应用程序停止工作...
我的问题是:
- 如何避免这个问题?
- 我应该增加这个参数的值吗?
- 增加这样的价值有什么缺点吗? (如果它长时间处于非活动状态,它可能始终是我的应用程序达到的值。:/)
- 有没有其他解决方案使用 Spring Boot(比如轮询或类似的东西)?
编辑
这里还有其他 similar/useful 个问题:
- Hibernate Communications Link Failure in Hibernate Based Java Servlet application powered by MySQL
- WARN SqlExceptionHelper:143 - SQL Error: 0, SQLState: 08S01- SqlExceptionHelper:144 - Communications link failure
- SQL Error: 0, SQLState: 08S01 Communications link failure
- https://serverfault.com/questions/89955/unable-to-connect-to-mysql-through-jdbc-connector-through-tomcat-or-externally
- Solving a "communications link failure" with JDBC and MySQL
从上面的参考资料我可以得出结论,一种解决方案是添加 C3p0 作为附加库并正确配置它以避免通信 link 错误..
这是我唯一的解决方案吗?有没有更多 "integrated" 和 Spring/Spring 启动的解决方案(即不添加外部库)?
我按照此处所述解决了问题:
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