MySQLNonTransientConnectionException: No operations allowed after statement closed
之所以会出现这个异常,是因为Mysql在5以后针对超长时间DB连接做了一个处理,那就是如果一个DB连接在无任何操作情况下过了8个小时 后,Mysql会自动把这个连接关闭。所以使用连接池的时候虽然连接对象还在但是链接数据库的时候会一直报这个异常。解决方法很简单在Mysql的官方网 站上就可以找到。
有两个方法,
第一种是在DB连接字符串后面加一个参数。
这样的话,如果当前链接因为超时断掉了,那么驱动程序会自动重新连接数据库。
jdbc:mysql://localhost:3306/makhtutat?autoReconnect=true
不过Mysql并不建议使用这个方法。因为第一个DB操作失败的后,第二DB成功前如果出现了重新连接的效果。这个失败操作将不会处于一个事务以内,第二DB操作如果成功的话,这个事务将被提交。
conn.createStatement().execute( "UPDATE checking_account SET balance = balance - 1000.00 WHERE customer='Smith'"); conn.createStatement().execute( "UPDATE savings_account SET balance = balance + 1000.00 WHERE customer='Smith'"); conn.commit();
当然如果出现了重新连接,一些用户变量和临时表的信息也会丢失。
另一种方法是Mysql推荐的,需要程序员手动处理异常。
- <span style="font-family:Microsoft YaHei;font-size:12px;">public void doBusinessOp() throws SQLException {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- int retryCount = 5;
- boolean transactionCompleted = false;
- do {
- try {
- conn = getConnection(); // assume getting this from a
- // javax.sql.DataSource, or the
- // java.sql.DriverManager
- conn.setAutoCommit(false);
- retryCount = 0;
- stmt = conn.createStatement();
- String query = "SELECT foo FROM bar ORDER BY baz";
- rs = stmt.executeQuery(query);
- while (rs.next()) {
- }
- all.close()
- transactionCompleted = true;
- } catch (SQLException sqlEx) {
- String sqlState = sqlEx.getSQLState();
- // 这个08S01就是这个异常的sql状态。单独处理手动重新链接就可以了。
- if ("08S01".equals(sqlState) || "40001".equals(sqlState))
- {
- retryCount--;
- } else {
- retryCount = 0;
- }
- } finally {
- all close:
- }
- } while (!transactionCompleted && (retryCount > 0));}
- }</span>
转自: http://blog.csdn.net/bedweather/article/details/6743951
相关推荐
解决:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
使用mysql+cp30连接池时,报错No operations allowed after connection closed。从报错信息来看,是connection断开导致的错误。在网上搜索后发现,较新版本的mysql配置了connection的默认时间,默认时间一般为8个...
directories that will not be allowed to be processed by Diskeeper. Event Logging ------------- Diskeeper records information about its activity in two ways. First, general information about ...
有些朋友新配置的环境或自己新写的程序运行时会碰到fatal error: Allowed memory size of 134217728 bytes exhausted错误,这种问题一个是程序写得不好导致的,另一个是环境配置不好也会出现,像php5.1.27+apache2.2.3...
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
主要介绍了php采用curl访问域名返回405 method not allowed提示的解决方法,需要的朋友可以参考下
MySQL导入sql脚本错误:2006 – MySQL server has gone away 到如一些小脚本很少报错,但最近导入一个10+M的SQL脚本,却重复报错: Error occured at:2014-03-24 11:42:24 Line no.:85 ...max_allowed_pa
Django 允许 CIDR 一个 Django 中间件,用于在 ALLOWED_HOSTS 中启用 CIDR IP 范围。快速开始安装 Django 允许 CIDR: pip install django-allow-cidr将中间件添加到您的 MIDDLEWARE_CLASSES(对于 Django < 1.10...
本文实例讲述了ThinkPHP提示错误Fatal error: Allowed memory size的解决方法。分享给大家供大家参考。具体分析如下: 如果你的ThinkPHP提示你:致命错误(Fatal error: Allowed memory size),根据网上说的提高...
解决JsonException: Max allowed object depth reached while trying to export from type UnityEngine.Vector3
Android WebView 报 Not allowed to load local resource错误的解决办法 博客地址:http://blog.csdn.net/yuzhiqiang_1993/article/details/76228541
这个存储库有助于增加令人敬畏的。关于Android-Speech-Bubble 是一个在 android ListView中使用 9 个补丁图像的演示的努力这个怎么运作! 好吧,可以在这里找到相关的博客条目: 执照特此授予任何人免费获得本软件...
下面写一下,这两个要是有no了,怎么恢复。... 一旦io为no了先看err日志,看看爆什么错,很可能是网络,也有可能是包太大收不了,这个时候主备上改max_allowed_packet这个参数。 如果是slave_sql_running
-27002 - No connections are allowed in Dynamic Server quiescent mode. ********************************************************** 4.3.6 关闭数据库 onmode –yuk 4.4 操作系统环境检查 4.4.1 检查G网...
从3GPP协议和UE端行为分析5G gmm cause #15的网络问题
NULL 博文链接:https://rayoo.iteye.com/blog/1973032
php Allowed memory size of 134217728 bytes exhausted
NULL 博文链接:https://yizhilong28.iteye.com/blog/1137285