Spring Boot不会恢复Mysql连接

时间:2017-01-10 22:31:00

标签: spring spring-boot

如果我的Spring Boot应用程序闲置得更长,MySql wait_timeout无法创建新连接并且应用程序无法使用。

2017-01-10 21:51:42.613 ERROR 26973 --- [http-nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : No operations allowed after connection closed.
2017-01-10 21:51:42.621 ERROR 26973 --- [http-nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: could not prepare statement; nested exception is org.hibernate.exception.JDBCConnectionException: could not prepare statement] with root cause

java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost

我查看过许多类似问题并添加了大部分建议,但问题仍然存在。我已经尝试了以下几行的组合。我的application.properties的相关部分是

spring.datasource.url = jdbc:mysql://localhost/mydb?autoReconnect=true&useSSL=false

spring.datasource.log-validation-errors=true
spring.datasource.tomcat.testOnBorrow=true
spring.datasource.tomcat.timeBetweenEvictionRunsMillis=30000
spring.datasource.tomcat.minEvictableIdleTimeMillis=60000
spring.datasource.tomcat.validationInterval=60000
spring.datasource.tomcat.validationQuery = SELECT 1
spring.datasource.tomcat.testWhileIdle=true
spring.datasource.tomcat.removeAbandoned=true
spring.datasource.tomcat.removeAbandonedTimeout=55

我也尝试过没有tomcat命名空间的上述行,但我相信 .tomcat。对于Spring Boot 1.4+是正确的。我也尝试过蛇案例属性名称(例如,空闲时测试)。

我似乎只是忽略了配置。

我正在使用

  • Spring Boot 1.4.3
  • Hibernate 5.2.6

1 个答案:

答案 0 :(得分:0)

终于明白了。我在Configuration类中创建了Datasource并手动设置了一些属性。

删除它只允许它使用以下配置属性

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