Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决
springboot + mybatis多数据库 + druid连接池配置成功。正常运行后,访问第一个数据库后访问第二个数据库,再去访问第一个数据库 会报错:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 8,451 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
### The error may exist in com/bwton/dist/dao/sys/DictDao.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select id,code,value,caption,status, create_user,create_time,update_user,update_time from dictionary where code = ? and value <> "!" and status = "1" order by value
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 8,451 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
; SQL []; Communications link failure
The last packet successfully received from the server was 8,451 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 8,451 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)
网上查了下,原因是
MySQL服务器默认的“wait_timeout”是28800秒即8小时(查看mysql实际wait_timeout可以用sql命令:show global variables like ‘wait_timeout’?,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。
解决方法:
- 1、修改my.cnf:
[mysqld]
wait_timeout=31536000
interactive_timeout=31536000
将过期时间修改为1年。 - 2、在连接URL上添加参数:&autoReconnect=true&failOverReadOnly=false
我的解决采用的是第二种方法,在URL后面加上&autoReconnect=true&failOverReadOnly=false
网上搜索参考自:
https://blog.csdn.net/a704397849/article/details/93797529
在此也感谢开发老表的协助。