一.报错的问题:

测试环境在做压力测试的时候爆出错误

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
INFO | jvm 1 | 2013/10/24 14:22:28 |
INFO | jvm 1 | 2013/10/24 14:22:28 | The last packet successfully received from the server was 26,071 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
INFO | jvm 1 | 2013/10/24 14:22:28 |

二.解决问题:

先看看MySQL服务器默认的“wait_timeout”是120秒,意味着如果一个连接的空闲时间超过120秒,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。
mysql> show global variables like 'wait_timeout';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| wait_timeout                | 120    |
+----------------------------+-------+

----------------------------------------------------------

MySQL连接如果8小时未使用,在查询使用到该连接会报:

异常名称:com.mysql.jdbc.CommunicationsException
异常信息: Communications link failure due to underlying exception

如果是MySQL5以前的版本,需要修改连接池配置中的URL,添加autoReconnect=true

如果是MySQL5 以后的版本,需要修改my.cnf(或者my.ini)文件,在[mysqld]后面添加

wait_timeout = 172800
interactive-timeout = 172800

单位都是秒,记得必须都添加,否则不起作用,通过show variables查看wait_timeout的值

-----------------------------------------

由于项目中使用的是c3p0连接池,c3p0配置需查看这些属性是否已经加上。

<!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->
<property name="testConnectionOnCheckin">true</property>

<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
等方法来提升连接测试的性能。Default: false -->
<property name="testConnectionOnCheckout">false</property>

<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod">60</property>

<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">60</property>

具体参数详见:http://blog.csdn.net/yougou_sully/article/details/13430405

官方参数详见:http://www.mchange.com/projects/c3p0/index.html

Communications link failure报错的处理的更多相关文章

  1. Java连接MySQL报错:CommunicationsException: Communications link failure

    现象: 报错:Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Co ...

  2. 项目启动报错:Communications link failure

    2017-12-29 10:43:19,776 ERROR [com.alibaba.druid.pool.DruidDataSource] - <init datasource error, ...

  3. springboot项目启动报错Communications link failure

    环境情况,MySQL版本如下: 报错情况如下(看上去是和数据库有关): com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communi ...

  4. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 数据库报错

    -- 查询mysql 数据库链接空闲时间持有最大空闲时间,单位为秒 SHOW VARIABLES WHERE VAriable_name = 'interactive_timeout'; -- 会出现 ...

  5. 【Azure 应用服务】App Service 无法连接到Azure MySQL服务,报错:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

    问题描述 App Service使用jdbc连接MySQL服务,出现大量的  Communications link failure: com.mysql.cj.jdbc.exceptions.Com ...

  6. SSH项目过一段时间之后再访问会报一次Could not open Hibernate session for transaction 异常,Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlyi,再重新方法即可访问成功(通常出现在过了一晚之后再去访问系统)

    前端时间到客户那去进行项目的上线测试,将项目部署好之后,运行都是正常的,可是每到了第二天早上访问的时候,就会报一个Could not open Hibernate session for transa ...

  7. Communications link failure的解决办法

    使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误: Communications link failure,The last packet successfully r ...

  8. jdbc 连接mysql Communications link failure的解决办法

    使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误: Communications link failure,The last packet successfully r ...

  9. 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 ...

随机推荐

  1. window下版本控制工具Git 客户端安装

    安装使用 1.下载msysgit http://code.google.com/p/msysgit/ 2.下载tortoisegit客户端安装 http://code.google.com/p/tor ...

  2. 遍历 集合 Dictionary 的时候修改集合 方法

    Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("1" ...

  3. C++ static内容小结

    C++中static总结比较好的博客:http://blog.csdn.net/laixingjun/article/details/9139839 http://blog.csdn.net/xiaj ...

  4. 在ubuntu下配置android开发环境

    http://developer.android.com/sdk/installing/index.html 基本上上面官网的链接可以解决所有问题,但是具体在安装过程中还是有一些坑. 说说具体流程 1 ...

  5. nodejs学习笔记之mongoDB

    这两天在学习nodejs,但是发现那本书nodejs入门指南上所用的好多方法都报错. 这里主要说下数据库部分 关于注册部分:书上创建数据库那里可能要小心点,用户名不存在的时候,下面调用save的对象要 ...

  6. Tkinter教程之Scrollbar篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811319 '''Tkinter教程之Scrollbar篇'''#Scrollbar(滚动条) ...

  7. 【bz2594】水管局长数据加强版

    题意: 给出一张n节点.m条代权无向边的无向联通图 和q个任务 1:询问一条x到y的路径 并使路径上最大权值最小 要求输出路径上最大权值 2:宣布x到y的路径报废题目保证该图永远联通 题解: 这是道凶 ...

  8. dedecms

    http://www.duodede.com/free/  [free templte] http://www.xiuzhanwang.net/d

  9. cocos2d-x图片变灰或者变亮

    //根据现有CCSprite,变亮和变灰 CCSprite* FlyLeaf::graylightWithCCSprite(CCSprite* oldSprite,bool isLight) {    ...

  10. homework-09

    这次作业主要考察C++11的简单用法,个人感觉这样的练习对我这种编程能力比较差的非常有用,加深了对C++11的理解. Lambda的用法 计算“Hello World!”中 a.字母‘e’的个数 b. ...