推荐链接:
总结——》【Java】
总结——》【Mysql】
总结——》【Spring】
总结——》【SpringBoot】
总结——》【MyBatis、MyBatis-Plus】
解决——》CommunicationsException:Communications link failure
- 1、操作
- 2、现象
- 3、原因
- 4、解决
- 1)执行sql调大wait_timeout的值
- 2)修改/etc/my.cnf调大wait_timeout的值
1、操作
正常查询数据库
2、现象
通信异常:通信链路故障
从服务器成功接收的最后一个数据包是66734毫秒前。最后一个成功发送到服务器的数据包是66732毫秒前。
2022-11-16 17:59:05.129 ERROR [10.122.139.93] [http-nio-8917-exec-9] [house-cms-0a7a8b5d-463497-1034] com.alibaba.druid.filter.logging.Slf4jLogFilter.statementLogError [line=149 ] : {conn-10020, pstmt-34465} execute error. SELECT id,title,dept_name,emp_code,emp_name,emp_phone,password,emp_mail,status,sort,type,remark,login_ip,login_time,is_delete,update_user,create_user,version,create_time,update_time FROM tb_user
WHERE is_delete=0
AND (emp_code = ?) limit 0,1
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 66,734 milliseconds ago. The last packet sent successfully to the server was 66,732 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3462)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3905)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2495)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1903)
at com.mysql.jdbc.PreparedStatement.execute$original$Qo3qQCn7(PreparedStatement.java:1242)
at com.mysql.jdbc.PreparedStatement.execute$original$Qo3qQCn7$accessor$m73OheMY(PreparedStatement.java)
at com.mysql.jdbc.PreparedStatement$auxiliary$qLH27S5e.call(Unknown Source)
at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3461)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459)
at com.alibaba.druid.wall.WallFilter.preparedStatement_execute(WallFilter.java:626)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459)
at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:497)
3、原因
Mysql的连接等待时间(wait_timeout)默认8小时。在wait_timeout时间内,mysql的连接(connection)处于等待状态,一旦超过这个时间,mysql就会关闭。但是在我们的项目中的连接池中仍存在合法的connection,当你的程序需要再次访问数据库时,就会出现以上异常,但是再重新访问,又能正常从数据库中获取到数据。
# 连接等待时间,单位为秒
# 默认8小时 = 28800s,windows最大24天,linux最大365天
show global variables like 'wait_timeout';
4、解决
1)执行sql调大wait_timeout的值
# mysql重启之后又会恢复成8小时
set global wait_timeout=1814400;
2)修改/etc/my.cnf调大wait_timeout的值
# 重启mysql服务之后生效,大约21天
wait_timeout=1814400