com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
The last packet successfully received from the server was 34 ms ago.
The last packet sent successfully to the server was 34 ms ago.
根因:MySQL 預設 wait_timeout = 28800 (8 小時)。應用閒置超過 8 小時 (如夜間無流量),MySQL 主動斷開連線,連線池仍持有失效連線,晨間首請求報錯。
SET GLOBAL wait_timeout = 31536000; -- 365 天 (最大值)
SET GLOBAL interactive_timeout = 31536000;
# Linux: /etc/my.cnf 或 /etc/mysql/my.cnf
# Windows: my.ini
[mysqld]
wait_timeout = 31536000
interactive_timeout = 31536000
修改後重啟 MySQL:systemctl restart mysqld。
SHOW VARIABLES LIKE '%timeout%';
+--------------------------+----------+
| Variable_name | Value |
+--------------------------+----------+
| connect_timeout | 10 |
| wait_timeout | 31536000 |
| interactive_timeout | 31536000 |
| ... | ... |
+--------------------------+----------+
spring.datasource.hikari:
maximum-pool-size: 20
minimum-idle: 5
idle-timeout: 300000 # 5 分鐘閒置回收
max-lifetime: 1800000 # 30 分鐘強制換新
connection-timeout: 30000 # 獲取連線超時 30s
validation-timeout: 5000 # 驗證超時 5s
leak-detection-threshold: 60000 # 洩漏檢測 60s
connection-test-query: SELECT 1 # MySQL 5.7+ 可省略 (內建 isValid)
# 關鍵:定期驗證閒置連線
keepalive-time: 0 # 0=關閉,若網絡設備有空閒斷開需設 < wait_timeout
spring.datasource.druid:
initial-size: 5
max-active: 20
min-idle: 5
max-wait: 30000
time-between-eviction-runs-millis: 60000 # 每分鐘檢查一次
min-evictable-idle-time-millis: 300000 # 閒置 5 分鐘可被驅逐
validation-query: SELECT 1
test-while-idle: true # 閒置時驗證 (核心)
test-on-borrow: false # 借用時不驗證 (效能)
test-on-return: false
max_connections),導致真實業務無法連線。wait_timeout ≥ 連線池 maxLifetime + 閒置檢查間隔。testWhileIdle / keepaliveTime,主動探活。sysctl -w net.ipv4.tcp_keepalive_time=60 (Linux 內核參數)。Threads_connected、Aborted_connects、連線池指標。「Server 端放寬 + Client 端主動探活」雙管齊下,徹底杜絕晨間第一請求失敗。