可能考虑使用较短的maxLifetime值——hikari连接池spring boot

San*_*jog 15 java jpa spring-data-jpa spring-boot hikaricp

启动我的 SpringBoot 应用程序后,在服务器启动的几分钟内出现异常。外部没有使用任何 HikariPool 配置,Spring Boot 默认使用 HikariPool 这是我在控制台中得到的错误:

2020-02-20 03:16:23 - HikariPool-4 - Failed to validate connection 
com.mysql.cj.jdbc.ConnectionImpl@4c4180c8 (No operations allowed after connection closed.). 
Possibly consider using a shorter maxLifetime value.
2020-02-20 03:16:28 - HikariPool-4 - Failed to validate connection 
com.mysql.cj.jdbc.ConnectionImpl@679c2f50 (No operations allowed after connection closed.). 
Possibly consider using a shorter maxLifetime value.
2020-02-20 03:16:33 - HikariPool-4 - Failed to validate connection 
com.mysql.cj.jdbc.ConnectionImpl@16083061 (No operations allowed after connection closed.). 
Possibly consider using a shorter maxLifetime value.
2020-02-20 03:16:38 - HikariPool-4 - Failed to validate connection 
com.mysql.cj.jdbc.ConnectionImpl@4fcaf421 (No operations allowed after connection closed.). 
Possibly consider using a shorter maxLifetime value.
2020-02-20 03:16:43 - HikariPool-4 - Failed to validate connection 
com.mysql.cj.jdbc.ConnectionImpl@33df5d54 (No operations allowed after connection closed.). 
Possibly consider using a shorter maxLifetime value.
2020-02-20 03:16:48 - HikariPool-4 - Failed to validate connection 
com.mysql.cj.jdbc.ConnectionImpl@373d288c (No operations allowed after connection closed.). 
Possibly consider using a shorter maxLifetime value.
2020-02-20 03:16:48 - SQL Error: 0, SQLState: 08003
2020-02-20 03:16:48 - HikariPool-4 - Connection is not available, request timed out after 
30156ms.
2020-02-20 03:16:48 - No operations allowed after connection closed.
2020-02-20 03:16:48 - Servlet.service() for servlet [dispatcherServlet] in context with path 
[] threw exception [Request processing failed; nested exception is 
org.springframework.dao.DataAccessResourceFailureException: Unable to acquire JDBC 
Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to 
acquire JDBC Connection] with root cause
Run Code Online (Sandbox Code Playgroud)

Kar*_*ert 18

问题是 spring.datasource.hikari.maxLifetime 的默认值(30 分钟 - https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby)高于数据库的“wait_timeout”(10 分钟我的情况)。
所以你有两个选择,要么将 hikari.maxLifetime 减少到 10 分钟以下,要么增加数据库的“wait_timeout”属性。

  • spring.datasource.hikari.max-lifetime=600000(以毫秒为单位) (4认同)
  • 您知道我们必须根据该属性来设置该值吗? (2认同)

小智 5

您可以在 application.properties 文件中设置如下值

spring.datasource.hikari.maxLifeTime : 600000 #10 分钟等待时间