首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >可以考虑使用较短的maxLifetime值- hikari连接池spring boot

可以考虑使用较短的maxLifetime值- hikari连接池spring boot
EN

Stack Overflow用户
提问于 2020-02-20 07:33:35
回答 3查看 33.2K关注 0票数 20

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

代码语言:javascript
运行
复制
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
EN

回答 3

Stack Overflow用户

发布于 2020-02-25 00:29:07

问题是spring.datasource.hikari.maxLifetime的默认值(30分钟- https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby)高于数据库的"wait_timeout“(在我的例子中是10分钟)。

因此,您有两个选择,要么将hikari.maxLifetime降低到10分钟以下,要么增加数据库的"wait_timeout“属性。

票数 22
EN

Stack Overflow用户

发布于 2020-06-24 01:15:09

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

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

票数 7
EN

Stack Overflow用户

发布于 2022-01-11 09:52:23

在我的例子中,我通过这个设置解决了这个问题

代码语言:javascript
运行
复制
@Configuration
public class HikariSetting{

    @Bean
    public HikariConfig config() {
        HikariConfig hikariConfig = new HikariConfig();
        
        // other setting
        
        hikariConfig.addDataSourceProperty("socketTimeout", 600000);
        hikariConfig.setMaxLifetime(600000);
        
        return hikariConfig;
    }
    
}

参考this

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60310858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档