本人用struts+jDBC+tomcat+mysql 做了个小型网站,刚开始挂上去时没有问题,等过三天左右就会报No operations allowed after connection closed.错误
报错如下:javax.servlet.ServletException: java.sql.SQLException: No operations allowed after connection closed.
Connection was closed due to the following exception:
** BEGIN NESTED EXCEPTION **
java.sql.SQLException
MESSAGE: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Software caused connection abort: recv failed
后经网上查找发现原因:
出现这种异常的原因是:Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该 connection。这就是问题的所在,在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有 Client请求connection,C3P0将该失效的Connection提供给Client,将会造成上面的异常。
自己测试了一下,在mysql 的配置文件my.ini中在
[mysqld]下添加
wait_timeout=100 服务器关闭交互式连接前等待活动的秒数
interactive_timeout=100 服务器关闭非交互连接之前等待活动的秒数
两个参数,即将mysql默认的链接等待时间由8小时改为100秒,登录网站,确实有如上错误出现。
解决方案:1、换一下JDBC驱动,JDBC3.1.0-alpha及以前版本会出现此问题,下载新的JDBC驱动
2、使用hibernate配置:
使用hibernate:
<property name="connection.autoReconnect">true</property> <!--这个是最主要的-->
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
加入以上property,可解决此问题,如果未使用hibernate等持久化管理框架,可在mysql的url中加入autoReconnect=true,这样就可以解决(源自iteye 信仰的力量)
抱歉上述方法可行性有问题,现改变:
原因很简单。在对数据库操作结束后关闭连接是正确的做法,没什么大问题。至于出现:No operations allowed after connection closed。这样的问题原因只有一个,你这里和数据库的连接Connection是一个Static的,程序共享这一个Connection。所以第一次对数据库操作没问题,当把Connection关闭后,第二次还想操作数据库时Connection肯定不存在了。(源自CSDN 小荷初现)
有没有更好的方法,请各位指点一下
分享到:
相关推荐
解决:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
使用mysql+cp30连接池时,报错No operations allowed after connection closed。从报错信息来看,是connection断开导致的错误。在网上搜索后发现,较新版本的mysql配置了connection的默认时间,默认时间一般为8个...
java.sql.SQLException: Operation not allowed after ResultSet closed java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection should be passed in
设置隐试打开PPT报错 Hiding the application window is not allowed
Android WebView 报 Not allowed to load local resource错误的解决办法 博客地址:http://blog.csdn.net/yuzhiqiang_1993/article/details/76228541
keil 编译时出现错误和警告总结 中国电子DIY之家
- Video playback, Hard disk and CD/DVD test 'no operations' error reporting changed. - When BurnInTest crashes, it will not generate a "minidump" file. Minidumps will need to be sent to Microsoft ...
$ npm install is-retry-allowed 用法 import isRetryAllowed from 'is-retry-allowed' ; isRetryAllowed ( { code : 'ETIMEDOUT' } ) ; //=> true isRetryAllowed ( { code : 'ENOTFOUND' } ) ; //=> false ...
informix point in time table level restore
前端项目-jquery.allowed-chars,jQuery plugin to restrict users for typing only allowed chars for specified element
Performance criteria B description: During the test, degradation of performance is allowed. However, no change of operating state or stored data is allowed to persist after the test. After the test, ...
keil常见错误 keil常见错误 keil常见错误 keil常见错误
ffmpeg命令
解决SQLSERVER无法安装的问题,提示文件挂起的错误,通过该东东能彻底解决。
使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决
Contents Contents ii List of Tables x List of Figures xiv 1 Scope 1 2 Normative references 2 3 Terms and definitions 3 4 General principles 7 4.1 Implementation compliance . ....4.2 Structure of this ...
NULL 博文链接:https://yizhilong28.iteye.com/blog/1137285
There is no name validation. If PHP can't find an expected ; directive because it is not set or is mistyped, a default value will be used. ; The value can be a string, a number, a PHP constant (e.g....
- FIX: After deleting the selected points in the TFlexPanel.DeleteSelectedPoints the all figures with one point also deleted. - FIX: When the curve contain more then one figure and they were all ...
Contents Contents iii List of Tables xi List of Figures xv 1 General 1 1.1 Scope . . . . ....1.2 Normative references ....1.3 Terms and definitions ....1.4 Implementation compliance ....1.5 Structure of this ...