Skip to content

还是没解决

1. 背景

最近发现使用 hikariCP 连接MYSQL数据库(使用Docker安装8.0.20)时,总是出现错误如下:

Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@4c4180c8 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.

检查数据库的 mysqlx_wait_timeoutwait_timeout 参数是 28800 秒, hikari的连接配置参数 spring.datasource.hikari.max-lifetime 参数是 1800000 毫秒;正常来说应该不会报这个错误的。

2. 解决

最后在 hikariCP文档中发现这一段描述

HikariCP relies on accurate timers for both performance and reliability. It is imperative that your server is synchronized with a time-source such as an NTP server. Especially if your server is running within a virtual machine. Why? Read more hereDo not rely on hypervisor settings to "synchronize" the clock of the virtual machine. Configure time-source synchronization inside the virtual machine. If you come asking for support on an issue that turns out to be caused by lack time synchronization, you will be taunted publicly on Twitter.

主要意思就是服务器(这里指数据库服务器,镜像中运行的话指容器中的时间)中的需与时间源(例如NTP服务器)同步。

  • 查看容器时间

这里的 mysq8 是容器的别名

$ docker exec -it mysq8 date

显示的是

Wed Nov 25 07:26:44 UTC 2020

实际外部时间是

Wed Nov 25 15:26:49 CST 2020

数据库的时区

show variables like '%time_zone%';

| system_time_zone | CST   |
|  time_zone | SYSTEM |

命令记录

mv /etc/localtime /etc/localtime.backup
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

或者直接复制
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime