HikariPool连接池配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HikariPool连接池配置相关的知识,希望对你有一定的参考价值。
参考技术A 2020-xx-xx xx:xx:xx.xxx WARN 13301 --- [o-8600-exec-200] c.z.h.p.PoolBase : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@37866d7 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.原因:
解决方法:
参考链接:
1 https://my.oschina.net/kevin2kelly/blog/2873468
2 https://blog.csdn.net/qq_40930171/article/details/107044138
可能考虑使用更短的 maxLifetime 值 - hikari 连接池 spring boot
【中文标题】可能考虑使用更短的 maxLifetime 值 - hikari 连接池 spring boot【英文标题】:Possibly consider using a shorter maxLifetime value - hikari connection pool spring boot 【发布时间】:2020-06-04 05:49:44 【问题描述】:启动我的 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
【问题讨论】:
什么是root cause
?
这里是完整的异常堆栈:
【参考方案1】:
就我而言,我通过这个设置解决了这个问题
@Configuration
public class HikariSetting
@Bean
public HikariConfig config()
HikariConfig hikariConfig = new HikariConfig();
// other setting
hikariConfig.addDataSourceProperty("socketTimeout", 600000);
hikariConfig.setMaxLifetime(600000);
return hikariConfig;
参考this
【讨论】:
您不需要定义 bean,只需在application.properties
中提供适当的属性,建议通过替换 bean 来解决此问题不是正确的解决方案。【参考方案2】:
您可以在 application.properties 文件中设置如下值
spring.datasource.hikari.maxLifeTime : 600000 #10分钟等待时间
【讨论】:
【参考方案3】:问题在于 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(以毫秒为单位) 我使用 wait_timeout = 300(5 分钟)和 max-lifetime = 600000(10 分钟)进行了测试。但是我无法收到此错误。你知道为什么吗? @CanattoFilipe 当应用程序尝试使用它假定处于活动状态的旧连接池(早于 DB wait_timeout)与 DB 交互时,会发生错误。所以我认为你应该试着模拟一下。 RAVI SHANKAR 的答案是最好的,因为它告诉我们“这样做是为了解决”而不是“找出如何解决”以上是关于HikariPool连接池配置的主要内容,如果未能解决你的问题,请参考以下文章
聊聊hikari连接池的leakDetectionThreshold
HikariPool-1 - Connection is not available, request timed out after XXXXms.
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException:尝试使用 Redshift 设置 HikraCP 时无法初始化池
可能考虑使用更短的 maxLifetime 值 - hikari 连接池 spring boot
Springboot - 包括 JPA 依赖项并运行“mvn clean install”失败,HikariPool-1 - 池初始化期间出现异常