问题遇到的现象和发生背景
应用后台日志发现错误:
2022-05-01 06:12:23.422 [demo-app: N/A] [ERROR] com.zaxxer.hikari.pool.HikariPool - HikariPool-lic-c-1 - Error thrown while acquiring connection from data source
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet successfully received from the server was 5,004 milliseconds ago. The last packet sent successfully to the server was 5,004 milliseconds ago.
问题相关代码,请勿粘贴截图
没有代码,
数据库连接池配置是:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://192.123.0.71:3306/lk?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowMultiQueries=true
username: root
password: root
pool-name: HikariPool-lk
#连接最大存活时间,默认值30分钟.设置应该比mysql设置的超时时间短
max-lifetime: 30000
#最大连接数,小于等于0会被重置为默认值10;大于零小于1会被重置为minimum-idle的值
maximum-pool-size: 30
#连接超时时间:毫秒,小于250毫秒,否则被重置为默认值30秒
connection-timeout: 30000
#最小空闲连接,默认值10,小于0或大于maximum-pool-size,都会重置为maximum-pool-size
minimum-idle: 10
# 默认是600000毫秒,即10分钟。如果idleTimeout+1秒>maxLifetime 且 maxLifetime>0,则会被重置为0;
# 如果idleTimeout!=0且小于10秒,则会被重置为10秒。如果idleTimeout=0则表示空闲的连接在连接池中永远不被移除
# 只有当minimumIdle小于maximumPoolSize时,这个参数才生效,当空闲连接数超过minimumIdle,而且空闲时间超过idleTimeout,则会被移除。
idle-timeout: 28000
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
运行结果及报错内容
2022-05-01 06:12:23.422 [demo-app: N/A] [ERROR] com.zaxxer.hikari.pool.HikariPool - HikariPool-lic-c-1 - Error thrown while acquiring connection from data source
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet successfully received from the server was 5,004 milliseconds ago. The last packet sent successfully to the server was 5,004 milliseconds ago.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:1557)
at com.mysql.cj.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:1527)
at com.zaxxer.hikari.pool.PoolBase.setupConnection(PoolBase.java:402)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:363)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
at com.zaxxer.hikari.pool.HikariPool.access$100(HikariPool.java:71)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:725)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:711)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.mysql.cj.core.exceptions.CJCommunicationsException: Communications link failure
The last packet successfully received from the server was 5,004 milliseconds ago. The last packet sent successfully to the server was 5,004 milliseconds ago.
at sun.reflect.GeneratedConstructorAccessor130.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:60)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:99)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:139)
at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:155)
at com.mysql.cj.mysqla.io.MysqlaProtocol.readPacket(MysqlaProtocol.java:546)
at com.mysql.cj.mysqla.io.MysqlaProtocol.checkErrorPacket(MysqlaProtocol.java:712)
at com.mysql.cj.mysqla.io.MysqlaProtocol.sendCommand(MysqlaProtocol.java:651)
at com.mysql.cj.mysqla.MysqlaSession.sendCommand(MysqlaSession.java:518)
at com.mysql.cj.mysqla.MysqlaSession.queryServerVariable(MysqlaSession.java:1318)
at com.mysql.cj.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:1545)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at com.mysql.cj.core.io.ReadAheadInputStream.fill(ReadAheadInputStream.java:107)
at com.mysql.cj.core.io.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:150)
at com.mysql.cj.core.io.ReadAheadInputStream.read(ReadAheadInputStream.java:180)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at com.mysql.cj.core.io.FullReadInputStream.readFully(FullReadInputStream.java:64)
at com.mysql.cj.mysqla.io.SimplePacketReader.readHeader(SimplePacketReader.java:66)
at com.mysql.cj.mysqla.io.TimeTrackingPacketReader.readHeader(TimeTrackingPacketReader.java:54)
at com.mysql.cj.mysqla.io.MultiPacketReader.readHeader(MultiPacketReader.java:57)
at com.mysql.cj.mysqla.io.MysqlaProtocol.readPacket(MysqlaProtocol.java:540)
我的解答思路和尝试过的方法
检查配置, 调整数据库端的 timeout 配置; 还是一样的结果。
不懂,为什么出现有 5,004 milliseconds 的错误, 因为我并没有设置 5s;
我实际设置的是30s, 如果是5s 就读超时,那么如何解释?
我想要达到的结果
不出现 这个错误: