Spring 启动 & mysql - 连接关闭

Spring boot & mysql - connections closing

我们有一个应用程序需要查询 2 个 MySQL 个数据库(每个 DC 一个)。我们注意到我们在查询数据库后不断关闭和打开连接,这似乎对性能有影响。

HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.connectionImpl@xxxx (no operations allowed after connection closed.).  Possibly consider using a shorter maxLifetime value.
HikariPool-1 - Closing connection com.mysql.cj.jdbc.connectionImpl@xxxx (connection is dead)
...
HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.connectionImpl@xxxx (no operations allowed after connection closed.).  Possibly consider using a shorter maxLifetime value.
HikariPool-1 - Closing connection com.mysql.cj.jdbc.connectionImpl@xxxx (connection is dead)
...
(repeated per number of connections)

随后再次添加连接,我们可以看到池统计信息证实了这一点。

HikariPool-1 - Added connection com.mysql.cj.jdbc.connectionImpl@xxxx
HikariPool-1 - Added connection com.mysql.cj.jdbc.connectionImpl@xxxx
HikariPool-1 - Added connection com.mysql.cj.jdbc.connectionImpl@xxxx
HikariPool-1 - pool stats (total=23, active=0, idle=23, waiting=0)
...
HikariPool-1 - Added connection com.mysql.cj.jdbc.connectionImpl@xxxx
HikariPool-1 - Added connection com.mysql.cj.jdbc.connectionImpl@xxxx
HikariPool-1 - Added connection com.mysql.cj.jdbc.connectionImpl@xxxx
HikariPool-1 - pool stats (total=26, active=0, idle=26, waiting=0)
...
HikariPool-1 - pool stats (total=30, active=0, idle=30, waiting=0)
...
etc

有人以前看过这个吗?我试过更改 maxLifeTime 值和 idleTimeout 但没有效果。我也尝试添加 connectionTestQuery = SELECT 1,但问题仍然存在。

我的配置:

申请-dev.yml

spring:
   datasource1:
      driverClassName: com.mysql.jdbc.Driver
      jdbcUrl: XXXX
      password: xxxx
      username: xxxx

      maximum-pool-size: 40
      test-while-idle: true
      test-on-borrow: false

      connection-timeout: 5000
      wait-timeout: 5000
   datasource2:
      driverClassName: com.mysql.jdbc.Driver
      jdbcUrl: XXXX
      password: xxxx
      username: xxxx

      maximum-pool-size: 40
      test-while-idle: true
      test-on-borrow: false

      connection-timeout: 5000
      wait-timeout: 5000

我们知道连接关闭的原因。我们听取了供应商关于数据库等待超时的消息,他们说这是 MySQL 默认值(28800 秒(8 小时))……事实并非如此。

运行:

SHOW VARIABLES LIKE 'wait_%';

显示 wait_timeout 已设置为 30(30seconcds)。

对我们来说,解决方法是通过修改配置并重新启动数据库来增加数据库上的 wait_timeout。

我们也开始使用hikari keepalivetime

光: 保活时间:30000