Spring Boot JPA - 配置自动重新连接 [英] Spring Boot JPA - configuring auto reconnect

查看:34
本文介绍了Spring Boot JPA - 配置自动重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不错的 Spring Boot JPA Web 应用程序.它部署在 Amazon Beanstalk 上并使用 Amazon RDS 来持久化数据.然而,它不经常使用,因此在一段时间后失败,出现这种异常:

I have a nice little Spring Boot JPA web application. It is deployed on Amazon Beanstalk and uses an Amazon RDS for persisting data. It is however not used that often and therefore fails after a while with this kind of exception:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功接收的最后一个数据包是 79,870,633 毫秒前.
最后一个成功发送到服务器的数据包是在 79,870,634 毫秒之前.比服务器配置的wait_timeout"值长.您应该考虑在应用程序中使用之前使连接有效性过期和/或测试连接有效性,增加客户端超时的服务器配置值,或使用连接器/J 连接属性autoReconnect=true"来避免此问题.

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 79,870,633 milliseconds ago.
The last packet sent successfully to the server was 79,870,634 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.

我不确定如何配置此设置,也无法在 http://spring.io 上找到相关信息(虽然是一个非常好的网站).有哪些想法或信息提示?

I am not sure how to configure this setting and can not find information on it on http://spring.io (a very good site though). What are some ideas or pointers to information?

推荐答案

我假设 boot 正在为您配置 DataSource.在这种情况下,由于您使用的是 MySQL,您可以将以下内容添加到 application.properties 最高 1.3

I assume that boot is configuring the DataSource for you. In this case, and since you are using MySQL, you can add the following to your application.properties up to 1.3

spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1

正如 djxak 在评论中所指出的,1.4+ 为 Spring Boot 支持的四个连接池定义了特定的命名空间:tomcathikaridbcp, dbcp2(dbcp 从 1.5 开始不推荐使用).您需要检查您使用的是哪个连接池并检查该功能是否受支持.上面的例子是针对 tomcat 的,所以你必须在 1.4+ 中这样写:

As djxak noted in the comment, 1.4+ defines specific namespaces for the four connections pools Spring Boot supports: tomcat, hikari, dbcp, dbcp2 (dbcp is deprecated as of 1.5). You need to check which connection pool you are using and check if that feature is supported. The example above was for tomcat so you'd have to write it as follows in 1.4+:

spring.datasource.tomcat.testOnBorrow=true 
spring.datasource.tomcat.validationQuery=SELECT 1

请注意,autoReconnect 的使用是不推荐:

不推荐使用此功能,因为当应用程序不能正确处理 SQLExceptions 时,它会产生与会话状态和数据一致性相关的副作用,并且仅在您无法配置应用程序来处理时使用由死连接和陈旧连接正确导致的 SQLExceptions.

The use of this feature is not recommended, because it has side effects related to session state and data consistency when applications don't handle SQLExceptions properly, and is only designed to be used when you are unable to configure your application to handle SQLExceptions resulting from dead and stale connections properly.

这篇关于Spring Boot JPA - 配置自动重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆