最近的一个项目在Hibernate使用C3P0的连接池,数据库为Mysql。开发测试没有问题,在运行中每个一段长的空闲时间就出现异常:

java 代码
org.hibernate.exception.JDBCConnectionException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
.......
Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Broken pipe
STACKTRACE:
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
......
** END NESTED EXCEPTION **

查看了Mysql的文档,以及Connector/J的文档以及在线说明发现,出现这种异常的原因是:

Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection。这就是问题的所在,在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有Client请求connection,C3P0将该失效的Connection提供给Client,将会造成上面的异常。

解决的方法有3种:

  1. 增加wait_timeout的时间。
  2. 减少Connection pools中connection的lifetime。
  3. 测试Connection pools中connection的有效性。

当然最好的办法是同时综合使用上述3种方法,下面就DBCP和C3P0分别做一说明,假设wait_timeout为默认的8小时

DBCP增加以下配置信息:

//set to 'SELECT 1'
validationQuery = "SELECT 1"
//set to 'true'
testWhileIdle = "true"
//some positive integer
timeBetweenEvictionRunsMillis = 3600000
//set to something smaller than 'wait_timeout'
minEvictableIdleTimeMillis = 18000000
//if you don't mind a hit for every getConnection(), set to "true"
testOnBorrow = "true"

C3P0增加以下配置信息:

//获取connnection时测试是否有效
testConnectionOnCheckin = true
//自动测试的table名称
automaticTestTable=C3P0TestTable
//set to something much less than wait_timeout, prevents connections from going stale
idleConnectionTestPeriod = 18000
//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out
maxIdleTime = 25000
//if you can take the performance 'hit', set to "true"
testConnectionOnCheckout = true

更多的配置信息大家可以查看C3P0文档,Connector/J文档,以及DBCP的文档。

Communications link failure due to underlying exception异常处理(转)的更多相关文章

  1. 解决MySQL连接超时Communications link failure due to underlying exception

    最近在用一个MySQL的Java连接池的过程中,连接一晚上不释放,第二天就会造成超时的错误,查了一下原因,原来是因为MySQL默认的空闲等待时间是8个小时,一旦空闲超过8个小时,就会抛出异常.异常文本 ...

  2. 解决mysql连接异常—-com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception

    DBCP连接池连接MySql数据库时,一奇葩数据库设置为30秒内无请求自动断开.超时后链接无法关闭,活动链接数飞奔,最后挂掉. 网上找了一圈,一般是这三种,方法一pass,方法二测试无效可能设置错了吧 ...

  3. Communications link failure due to underlying exception: ** BEGIN NESTED EXC

    一是将 wait_timeout=31536000 interactive_timeout=31536000 将过期时间修改为1年. 二是在连接URL上添加参数:&autoReconnect= ...

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

  5. Communications link failure报错的处理

    一.报错的问题: 测试环境在做压力测试的时候爆出错误 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications l ...

  6. 【错误】:Could not open JDBC Connection for transaction; nested exception is: Communications link failure;The last packet sent successfully to the server was 1 milliseconds ago

    # #错误日志 2016-11-10 16:19:20,834 ERROR [org.quartz.core.JobRunShell] - Job DEFAULT.jobtask threw an u ...

  7. 异常-ERROR yarn.ApplicationMaster: User class threw exception: java.sql.SQLException: Communications link failure

    1 详细异常信息 ERROR yarn.ApplicationMaster: User class threw exception: java.sql.SQLException: Communicat ...

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

  9. Communications link failure异常解决

    一,异常现象 com.bill99.inf.ibatis.DBException: queryForList error::sqlId=orgOrderAssetsMapping.queryModel ...

随机推荐

  1. BZOJ1898 [Zjoi2005]Swamp 沼泽鳄鱼 矩阵

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1898 题意概括 有一个无向图. 其中,有许多条鱼在以循环的规律出现,比如循环在1,2,3这些点出现 ...

  2. BZOJ1195 [HNOI2006]最短母串 AC自动机 bfs

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 传送门 - BZOJ1195 题意概括 给出一堆串,然后求一个包含这些串的所有串的最短的中的字典序最小的. 题解 先造一个AC ...

  3. 6-5 移动的盒子 uva12657

    较为复杂的一题:有点类似6-1  但是分析完之后比6-1简单   就是按照思路模拟就好! 学会了双向链表   先初始化   link是关键 分析命令   可以大大简化代码  : 反转链表不用反转  改 ...

  4. 练习题|网络编程-socket开发

    1.什么是C/S架构? C指的是client(客户端软件),S指的是Server(服务端软件),C/S架构的软件,实现服务端软件与客户端软件基于网络通信. 2.互联网协议是什么?分别介绍五层协议中每一 ...

  5. HDU 1159 Common Subsequence 【最长公共子序列】模板题

    题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...

  6. 001.Keepalived简介

    一 Keepalived 定义 Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题.一个LVS服务会有2台服务器运行Keepalived,一台 ...

  7. LoRaWAN 1.1 网络协议规范 - 1 引言

    LoRaWAN 1.1 网络协议规范 LoRaWAN 1.1 版本封稿很久了也没有完整啃过一遍,最近边啃边翻译,趁着这个机会把它码下来. 如果觉得哪里有问题,欢迎留言斧正. 翻译不易,转载请申明出处和 ...

  8. python 一个函数让你决定你的二维码

    今天浏览网页时无意中看到了一个特别有趣的二维码,感觉挺有意思的,回来后自己查了一下,发现挺简单的,就分享一下,挺有意思的.该函数的主要流程就是MyQR库中的myqr.run()函数,将连接转化为二维码 ...

  9. C# 重启程序本身

    private static void Restart() { Thread thtmp = new Thread(new ParameterizedThreadStart(run)); object ...

  10. bzoj 3811: 玛里苟斯

    3811: 玛里苟斯 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 190  Solved: 95[Submit][Status][Discuss] ...