jpa 可以考虑使用较短的maxLifetime值- hikari连接池 Spring Boot

wgxvkvu9  于 2022-11-30  发布在  Spring
关注(0)|答案(3)|浏览(413)

在启动我的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
cld4siwp

cld4siwp1#

问题是spring.datasource.hikari.maxLifetime属性的默认值(默认值为30分钟,https://github.com/brettwooldridge/HikariCP#gear-configuration-knobs-baby)高于数据库的wait_timeout,在我的例子中是10分钟。
因此,您有两个选择:将hikari.maxLifetime减少到10分钟以下,或者增加数据库的wait_timeout属性。

92vpleto

92vpleto2#

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

spring.datasource.hikari.maxLifeTime : 600000 #10 minutes wait time
mf98qq94

mf98qq943#

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

@Configuration
public class HikariSetting{

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

引用此

相关问题