HikariCP - MYSQL 连接关闭后不允许操作。可能考虑使用更短的 maxLifetime 值
Posted
技术标签:
【中文标题】HikariCP - MYSQL 连接关闭后不允许操作。可能考虑使用更短的 maxLifetime 值【英文标题】:HikariCP - MYSQL No operations allowed after connection closed. Possibly consider using a shorter maxLifetime value 【发布时间】:2020-09-22 09:30:57 【问题描述】:我正在使用 mysql 数据库。它的默认等待超时时间为 28800
mysql> SHOW VARIABLES like '%timeout%';
+-----------------------------+----------+
| Variable_name | Value |
+-----------------------------+----------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| have_statement_timeout | YES |
| innodb_flush_log_at_timeout | 1 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| rpl_stop_slave_timeout | 31536000 |
| slave_net_timeout | 60 |
| wait_timeout | 28800 |
+-----------------------------+----------+
13 rows in set (0.00 sec)
mysql>
在我的 Spring Boot 应用程序中,hikariCP 用于管理数据库连接。我手动创建数据源 bean。
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceConfiguration extends HikariConfig
@Bean
public DataSource dataSource()
return new HikariDataSource(this);
这些是我的 application.properties
spring.datasource.jdbcUrl=jdbc:mysql://host.docker.internal:3306/demo
spring.datasource.username=sa
spring.datasource.password=test
spring.datasource.connection-timeout=3000
spring.datasource.minimum-idle=2
spring.datasource.maximum-pool-size=6
spring.datasource.idle-timeout=40000
spring.datasource.max-lifetime=900000
这些是我的启动日志
2020-06-03 15:44:12 - Starting DemoApplication on 48e5fb8724cb with PID 1 (/demo.jar started by root in /)
2020-06-03 15:44:12 - No active profile set, falling back to default profiles: default
2020-06-03 15:44:13 - For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
2020-06-03 15:44:13 - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-06-03 15:44:13 - Finished Spring Data repository scanning in 27ms. Found 0 JPA repository interfaces.
2020-06-03 15:44:15 - Tomcat initialized with port(s): 8000 (http)
2020-06-03 15:44:15 - Starting service [Tomcat]
2020-06-03 15:44:15 - Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-06-03 15:44:15 - Initializing Spring embedded WebApplicationContext
2020-06-03 15:44:15 - Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2020-06-03 15:44:15 - Root WebApplicationContext: initialization completed in 2581 ms
2020-06-03 15:44:15 - HikariPool-1 - Starting...
2020-06-03 15:44:15 - HikariPool-1 - Start completed.
2020-06-03 15:44:17 - Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-06-03 15:44:17 - Initializing AWS S3 Client...
2020-06-03 15:44:19 - spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-06-03 15:44:19 - Initializing ExecutorService 'applicationTaskExecutor'
2020-06-03 15:44:19 - ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2020-06-03 15:44:20 - 3 mappings in 'requestMappingHandlerMapping'
2020-06-03 15:44:20 - Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2020-06-03 15:44:20 - ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2020-06-03 15:44:20 - Tomcat started on port(s): 8000 (http) with context path ''
2020-06-03 15:44:20 - Started DemoApplication in 8.957 seconds (JVM running for 10.294)
【问题讨论】:
使用比 SHOW VARIABLES 列出的更短的 max-lifetime,例如 '%timeout%';,例如 spring.datasource.hikari.max-lifetime=20000 嗨,这个问题你解决了吗? 【参考方案1】:connect_timeout 以秒为单位定义
mysqld 服务器在响应错误握手之前等待连接数据包的秒数。默认值为 10 秒。
Hikari 文档说max-lifetime(毫秒)应该更少:
我们强烈建议设置此值,它应该比任何数据库或基础设施强加的连接时间限制短几秒钟。
因此您应该将其定义为例如 6 秒(或增加您的 MySQL 连接时间):
spring.datasource.max-lifetime=6000
【讨论】:
没有。此错误消息表明已建立的连接被 mysql 服务器关闭,因为至少 wait_timeout 秒没有活动。以上是关于HikariCP - MYSQL 连接关闭后不允许操作。可能考虑使用更短的 maxLifetime 值的主要内容,如果未能解决你的问题,请参考以下文章
java.sql.SQLException:连接关闭后不允许任何操作
MySQLNonTransientConnectionException:连接关闭后不允许操作。连接