一.报错的问题:

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

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. [HTML Q&A][转]使pre的内容自动换行

    <pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码 ...

  2. IIS部署网站

  3. MyBatis 物理分页

    MyBatis使用RowBounds实现的分页是逻辑分页,也就是先把数据记录全部查询出来,然在再根据offset和limit截断记录返回 为了在数据库层面上实现物理分页,又不改变原来MyBatis的函 ...

  4. again

    建立一个IPC连接: net use \\192.168.0.5\ipc$ "123456" /u:administrator

  5. cocos2d-x 详解之 CCSprite(精灵)- “CCSpriteBatchNode”和“CCSpriteFrameCache”

    帧动画-手动切换帧-批次渲染处理动画-纹理图片的本质 ------------------------------------------------------------------------- ...

  6. 定位程序问题的方法 -- clwu

    原本的标题的<定位程序代码的方法>,但问题有时候超出了自己代码的范围,而是别人的程序,所以今天想分享的是一个通用的分析问题(程序)的思路. 先来说一下在使用别人的程序(Vim)过程中遇到问 ...

  7. algorithm@ Shortest Path in Directed Acyclic Graph (O(|V|+|E|) time)

    Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths fr ...

  8. 你想成为优秀的Java程序员吗?

    Java是全世界最受欢迎的3大编程语言之一,它可以开发出许多实用的WEB应用程序和桌面应用程序,更重要的一点,Java是跨平台的语言——编写一次,可以再任何地方运行.另外,Java也很容易入门,如果你 ...

  9. HDU 5763 Another Meaning (kmp + dp)

    Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...

  10. HDU 2063 过山车(二分匹配入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.ne ...