问题:
Communications link failure due to underlying exception:
登录时,常常要登录2次,后来直接就不可以等了。
解决:
这个问题困惑了我很长时间,中间好像是在分析的,但是没有分析道重点上来,程序没有动过,那么数据库了,就应该是数据库
了。那么是数据那里链接的时间。对,是时间。
程序中的连接池中的链接,去取的时候,其实已经断开了。
借鉴(转)
在61家教网的运行阶段,发现很长时间没有访问后,都会出现 Communications link failure due to underlying exception的异常。但是在测试阶段一切都是正常的。
该系统采用了hibernate框架,使用C3P0的连接池。数据库为Mysql。出现的异常信息如下:
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: Software caused connection abort:
socket write error STACKTRACE: java.net.SocketException: Software
caused connection abort: socket write error at
java.net.SocketOutputStream.socketWrite0(Native Method) at
java.net.SocketOutputStream.socketWrite(Unknown Source) at
java.net.SocketOutputStream.write(Unknown Source) at
java.io.BufferedOutputStream.flushBuffer(Unknown Source) at
java.io.BufferedOutputStream.flush(Unknown Source) at
com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2692) at
com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2621) at
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1552) at
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666) at
com.mysql.jdbc.Connection.execSQL(Connection.java:2994) at
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)
at
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030)
at
org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
at
............
经查看相关资料,发现问题的原因如下:
Mysql服务器默认的“wait_timeout”是8小时,如果connection空闲超过8个小时,Mysql将自动断开该
connection。在C3P0
pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0中的该connection仍然存在,如果这时有
Client请求connection,C3P0将该断开的Connection提供给Client,将会造成上面的异常。
可以这么解决该问题:
使得Connection pools中connection的lifetime与Mysql的wait_timeout时间达到一致,或者测试Connection pools中connection的有效性。
在wait_timeout的时间为8小时的情况下:
C3P0增加以下配置信息:
-
-
preferredTestQuery = 'SELECT
1
'
-
-
idleConnectionTestPeriod =
18000
-
-
maxIdleTime =
25000
-
-
testConnectionOnCheckout =
true
-
-----------------------------------------------------------------------------------------------------------------------------------
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的值。
分享到:
相关推荐
com.mysql.jdbc.CommunicationsException Communications link failure due to underlying exception:
开发一个web应用,期间调用数据库时总是不成功,如果单独写一个类,并编译执行访问数据库,正常,但是用web应用来调这个类连接数据库,就会出现错误:com.mysql.jdbc.CommunicationsException: Communications link ...
A Brief Review of ChatGPT: Its Value and the Underlying GPT TechnologyA Brief Review of ChatGPT: Its Value and the Underlying GPT TechnologyA Brief Review of ChatGPT: Its Value and the Underlying GPT ...
Patt'Introduction to Computing Systems: From bits & gates to C & beyond', now in its second edition, is designed to give students a better understanding of computing early in their college careers in...
It includes methodologies and results related to link-level and system-level evaluations of LTE-Advanced. It provides understanding and insight into the advanced underlying technologies in LTE-...
Eclipse Rich Ajax Platform: Bringing Rich Client to the Web Paperback: 148 pages Publisher: Apress; 1 edition (December 29, 2008) Language: English ISBN-10: 1430218835 ISBN-13: 978-1430218838 ...
TCPIP协议原理教学课件:Chapter 3 Underlying Technology.ppt
It is aimed at executives who would like to use machine learning in their business and want to understand the underlying mechanics, and for anyone else who wants to understand more about the ...
而在不需要掉电保存数据的系统中,为了抑制电源纹波、电源干扰和负载变化,在电源端也会并接一个适当容量的电容。 然而电路中电容并不是越大越好,由于电容的储能作用,大容量的电容则可能延长系统地上电时间和下...
An overview of UEFI and underlying Platform Initialization (PI) specifications How to create UEFI applications and drivers Workflow to design the firmware solution for a modern platform Advanced ...
This book, evolving from lectures given to students with little knowledge of calculus, assumes no prior programming experience and demonstrates that most of the underlying ideas in intelligent ...
From geometric primitives to animation to 3D modeling to lighting, shading and texturing, Computer Graphics Through OpenGL®: From Theory to Experiments is a comprehensive introduction to computer ...
From geometric primitives to animation to 3D modeling to lighting, shading and texturing, Computer Graphics Through OpenGL®: From Theory to Experiments is a comprehensive introduction to computer ...
From geometric primitives to animation to 3D modeling to lighting, shading and texturing, Computer Graphics Through OpenGL®: From Theory to Experiments is a comprehensive introduction to computer ...
new ʻvisualizationsʼ are an attempt to explain the underlying information with a powerful visual impact. They take complex ideas and distil them into beautiful graphics revealing the ...
If you're viewing this document online, you can click any of the topics below to link directly to that section. 1. Tutorial tips 2 2. Introducing the JavaMail API 3 3. Reviewing related ...
Meet the Kinect goes beyond just the one device to impart the principles and terminology underlying the exciting field of volumetric computing that is now wide-open and accessible to the average ...
Your search marketing program must focus on your Web site’s underlying goals (such as sales) rather than fixating on your rankings in search results. The second edition updates all chapters with ...
The latter two are much faster but still can take a significant amount of time (up to 300 ms) on the older RS-232 electronics due to the computations involved. On the newer, USB electronics, the ...
A Practical Introduction to Machine Learning Concepts for Actuaries Alan Chalk, FIA, MSc, and Conan McMurtrie MSc Abstract Motivation. Supervised Learning - building predictive models based on past ...