zoukankan      html  css  js  c++  java
  • Communications link failure报错的处理

    一.报错的问题:

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

    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

  • 相关阅读:
    [硬件驱动_蓝牙]蓝牙Bluez的编程实现
    二分查找
    LeetCode-35.Search Insert Position
    LeetCode-34.Find First and Last Position of Element in Sorted Array
    LeetCode-704.Binary Search
    剑指offer-最小的k个数
    树的各种遍历
    LeetCode-912.Sort an Array
    排序
    LeetCode-209.Minimum Size Subarray Sum
  • 原文地址:https://www.cnblogs.com/pangblog/p/3395211.html
Copyright © 2011-2022 走看看