【问题标题】:Connection to Db dies after >4<24 in spring-boot jpa hibernate在 spring-boot jpa hibernate 中 >4<24 后与 Db 的连接终止
【发布时间】:2015-08-07 17:01:45
【问题描述】:

我有一个使用 spring-boot,jpa-hiberate 和 mysql 的应用程序。我收到此错误日志

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 56,006,037 milliseconds ago.  The last packet sent successfully to the server was 56,006,037 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.

这是我的 application.properties

# DataSource settings: set here configurations for the database connection
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = test
spring.datasource.password = test
spring.datasource.driverClassName = com.mysql.jdbc.Driver

# Specify the DBMS
spring.jpa.database = MYSQL

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate settings are prefixed with spring.jpa.hibernate.*
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy

为了解决这个问题,我可以使用

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

但我检查了它不是recommended。所以任何人都可以建议我应该怎么做才能克服这个错误

【问题讨论】:

  • 看看下面的帖子。 hibernatedb.blogspot.in/2009/05/…,这是一个评论链接,来自stackoverflow.com/questions/2077081/…
  • @KennethClark 我使用的是 spring-boot 而不是 spring,所以这些对我没有多大帮助。而且我对 spring 也不太了解。
  • Spring Boot 是基于 Spring 的,所以如果你使用 Spring Boot,当然你也在使用 Spring。如果你不知道的话,你应该阅读更多关于 Spring Boot 的基础知识。
  • 为什么不建议验证连接?不推荐使用 autoReconnect 属性。
  • @M.Deinum 我已经给出了上面的链接的原因。当应用程序不能正确处理 SQLExceptions 时,它会产生与会话状态和数据一致性相关的副作用。

标签: java mysql jpa spring-boot


【解决方案1】:

最简单的方法是在 JDBC url 中指定 autoReconnect 属性,尽管这不是推荐的方法。

spring.datasource.url = jdbc:mysql://localhost:3306/test?autoReconnect=true

当您有活动连接并且在事务期间发生某些事情并且将要重新连接时,这可能会出现问题。如果在事务开始时验证连接并在开始时获取新连接,则不会出现问题。

但是,在应用程序的生命周期内启用连接验证可能会更好。为此,您可以指定several properties

首先指定池允许的最大连接数。 (关于确定最大池大小的阅读this)。

spring.datasource.max-active=10

您可能还想指定初始连接数

spring.datasource.initial-size=5

接下来您要指定最小和最大空闲连接数。

spring.datasource.max-idle=5
spring.datasource.min-idle=1

要验证连接,您需要指定验证查询和验证时间。由于您希望定期验证,而不是在从池中检索连接时进行验证(这是为了防止池中的连接断开)。

spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=SELECT 1

注意:实际上不鼓励使用validation-query,因为 JDBC4 具有更好/不同的连接验证方式。 HikariCP 会在可用时自动调用 JDBC 验证方法。

现在您还要在连接空闲时进行验证,您需要指定要对连接运行此查询的频率以及何时将连接视为空闲。

spring.datasource.time-between-eviction-runs-millis=5000 (this is the default)
spring.datasource.min-evictable-idle-time-millis=60000 (this is also default)

这一切都应该触发对您的(空闲)连接的验证,并且当发生异常或空闲期已过时,您的连接将从池中删除。

假设您使用 Tomcat JDBC 作为连接池 this 很好地了解了配置内容和配置方法。

更新: Spring Boot 2.x 将默认连接池切换为 HikariCP 而不是 Tomcat JDBC。

【讨论】:

  • 谢谢@M。迪南。很棒的解释。抱歉迟到了。你救了我的一天。
  • 谢谢。 validation-query 是否每 time-between-eviction-runs-millismin-evictable-idle-time-millis 运行一次?
  • 从 Spring Boot 1.4.1 开始,many of these properties are ignored,您必须使用 spring.datasource.tomcat 来获取特定于 tomcat 的属性。
  • 有关 Tomcat 特定属性 (spring.datasource.tomcat.*) 及其默认值的说明,请参阅 Tomcat documentation
  • 这篇文章是否意味着要使用 url 进行自动重新连接,包括要设置的所有其他属性,或者在设置其他属性时不需要自动重新连接?我一直在使用具有休眠 5.1.3 的 spring boot 2.3.3。解决此问题的启动性能所需的最低属性是什么?
猜你喜欢
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多