【发布时间】: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