异常描述

这个异常通常有如下信息:

  1. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  2. The last packet successfully received from the server was 59,977 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.
  3. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  4. at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  5. at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  6. at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  7. at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
  8. at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
  9. at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3552)
  10. at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3452)
  11. at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3893)
  12. at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
  13. at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
  14. at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
  15. at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
  16. at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962)
  17. at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeQuery(DruidPooledPreparedStatement.java:227)
  18. at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:692)
  19. at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)
  20. ... 20 common frames omitted

原因分析

当数据库重启数据库空闲连接超过设置的最大timemout时间,数据库会强行断开已有的链接,最大timeout时间可以通过命令show global variables like "wait_timeout";查询:

  1. mysql> show global variables like "wait_timeout";
  2. +---------------+-------+
  3. | VARIABLE_NAME | VALUE |
  4. +---------------+-------+
  5. | wait_timeout | 28800 |
  6. +---------------+-------+
  7. 1 row in set (0.00 sec)

解决办法

为了解决这个异常,我们在配置数据库连接池的时候需要做一些检查连接有效性的配置,这里以Druid为例,相关配置如下(更多配置):

字段名 默认值 说明
validationQuery 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
validationQueryTimeout 单位:秒,检测连接是否有效的超时时间。底层调用jdbc Statement对象的void setQueryTimeout(int seconds)方法
testOnBorrow true 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
testOnReturn false 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
testWhileIdle false 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
timeBetweenEvictionRunsMillis 1分钟(1.0.14) 有两个含义:1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明

为了避免空闲时间过长超过最大空闲时间而被断开,我们设置三个配置:

  1. validationQuery: SELECT 1
  2. testWhileIdle: true
  3. timeBetweenEvictionRunsMillis: 28000

其中timeBetweenEvictionRunsMillis需要小于mysql的wait_timeout

但是这种方法无法避免重启的情况,不过一般数据库不会频繁重启,影响不大,如果非得频繁重启,可以通过设置testOnBorrow,即申请连接的时候先试一试连接是否可用,不过带来的影响就是性能降低,需要根据实际需求合理取舍。

异常解决:Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure的更多相关文章

  1. Oozie时出现Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure?

    不多说,直接上干货! 问题详情 [hadoop@bigdatamaster oozie--cdh5.5.4]$ bin/ooziedb.sh create -sqlfile oozie.sql -ru ...

  2. Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    很长的报错,截取   ERROR c.a.d.p.DruidDataSource - discard connection   com.mysql.jdbc.exceptions.jdbc4.Comm ...

  3. mysql 5.1超过默认8小时空闲时间解决办法(错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure)

    报错: MySQL第二天早上第一次连接超时报错, com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications lin ...

  4. sqoop从mysql导数据到hive报错:Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    背景 使用sqoop从mysql导数据到hive,从本地服务器是可以访问mysql的(本地服务器是hadoop集群的一个datanode),但是sqoop导数据的时候依然连接不上mysql 报错如下: ...

  5. Caused by: java.net.ConnectException: Connection refused/Caused by: java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    1.使用sqoop技术将mysql的数据导入到Hive出现的错误如下所示: 第一次使用命令如下所示: [hadoop@slaver1 sqoop--cdh5.3.6]$ bin/sqoop impor ...

  6. troubshooting-sqoop 导出 TiDB表数据报com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    2019-11-22 17:15:27,705 FATAL [IPC Server handler 13 on 44844] org.apache.hadoop.mapred.TaskAttemptL ...

  7. org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent succ

    数据库 没有开启  连接失败 org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause ...

  8. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法

    09:00:30.307 [http-8080-6] ERROR org.hibernate.transaction.JDBCTransaction -JDBC begin failed com.my ...

  9. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决

    感谢大佬:https://blog.csdn.net/a704397849/article/details/93797529 springboot + mybatis多数据库 + druid连接池配置 ...

随机推荐

  1. input响应慢问题解决办法

    input[file]标签的accept属性可用于指定上传文件的 MIME类型 . 例如,想要实现默认上传图片文件的代码,代码可如下: <input type="file" ...

  2. NLTK学习笔记(八):文法--词关系研究的工具

    [TOC] 对于一门语言来说,一句话有无限可能.问题是我们只能通过有限的程序来分析结构和含义.尝试将"语言"理解为:仅仅是所有合乎文法的句子的大集合.在这个思路的基础上,类似于 w ...

  3. 关于MATLAB处理大数据坐标文件2017527

    第一次提交数据: 今天用了8个特征,加上的这一个特征是 从3000条测试数据中测试失败的数据总结出来的树的数目为50再次使用3000条测试数据测试结果-- 结果不错: 99%但是运行官网数据结果分数- ...

  4. nvarchar 和varchar区别

    有时候设计字段的时候,碰到nvarchar和varchar时候,是有点犹豫.所以今天就来探个究竟把. (一)  varchar是非Unicode可变长度类型,nvarchar是Unicode编码可变长 ...

  5. yii 输出当前的sql语句

    <?php namespace app\controllers; use yii\web\Controller; use yii\data\Pagination; use app\models\ ...

  6. selenium及webdriver的原理

    主要内容转自:http://blog.csdn.net/ant_ren/article/details/7968582和http://blog.csdn.net/ant_ren/article/det ...

  7. 一些爬虫中的snippet

    1.tornado 一个精简的异步爬虫(来自tornado的demo) #!/usr/bin/env python import time from datetime import timedelta ...

  8. 本地jar上传到本地仓库

    转自:http://www.blogjava.net/fancydeepin/archive/2012/06/12/380605.html   thanks!! Maven 确确实实是个好东西,用来管 ...

  9. Ext 常用组件解析

    Ext 常用组件解析 Panel 定义&常用属性 //1.使用initComponent Ext.define('MySecurity.view.resource.ResourcePanel' ...

  10. JavaScript 语言基础

    js语言基础 一 基本知识 UniCode编码 区分大小写(HTML不区分/XHTML区分) Unicode转义序列 \uxxxx (\u加4位16进制表示) 注释 单行注释:// 多行注释:/* * ...