数据库为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增加以下配置信息:
-
//set to 'SELECT 1'
- preferredTestQuery = 'SELECT 1'
-
- idleConnectionTestPeriod = 18000
-
//set to something slightly less than wait_timeout, preventing 'stale' connections from being //handed out
- 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的值。
分享到:
相关推荐
本次异常的具体描述为:“Communications link failure due to underlying exception: **BEGINNESTED EXCEPTION** java.io.EOFException STACK TRACE: java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully...
这可能导致应用程序在尝试使用这些已断开的连接时遇到“Communications link failure due to underlying exception”异常。 解决这个问题的方法通常包括以下几点: 1. 调整`wait_timeout`值,增加到更长的时间,以...
错误信息可能表现为“***municationsException: Communications link failure due to underlying exception”以及一个***.ConnectException异常。这通常意味着无法建立到数据库服务器的网络连接。解决方法是确认IP...
The book is in two parts: the underlying structure of a computer, and programming in a high level language and programming methodology. To understand the computer, the authors introduce the LC-3 and ...
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 ...
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-...
His careful presentation of the underlying theories of fluids is still timely and applicable, even in these days of almost limitless computer power. This reissue ensures that a new generation of ...
but the time when loss first occurs will be different from one experiment to the next due to the randomness in the emission process. 22. Five generic tasks are error control, flow control, ...
After detection, the tracking of the multilevel patch features is closely coupled to the underlying extended Kalman filter (EKF) by directly using the intensity errors as innovation term during the ...
Visualizing data in plots and figures exposes the underlying patterns in the data and provides insights. Good visualizations also help you communicate your data to others, and are useful to data ...
an underlying two-dimensional cluster feature, we propose a two-dimensional sparse Bayesian learning method without a priori knowledge of two-dimensional angular spread patterns. It essentially ...
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 ...
This textbook enables readers to design digital systems using the modern HDL approach while ensuring they have a solid foundation of knowledge of the underlying hardware and theory of their designs....
This textbook enables readers to design digital systems using the modern HDL approach while ensuring they have a solid foundation of knowledge of the underlying hardware and theory of their designs....
Building upon Mark Sobell's A Practical Guide to the UNIX System, this book provides step-by-step tutorials for OS X users eager to get the most from the underlying UNIX system. Peter Seebach and ...
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 ...