Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
来源:互联网 发布:淘宝客服中心组织架构 编辑:程序博客网 时间:2024/06/07 06:38
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推荐的,需要程序员手动处理异常。
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));}}
0 0
- Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
- Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
- Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
- Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
- MySQLNonTransientConnectionException: No operations allowed after statement closed
- MySQLNonTransientConnectionException: No operations allowed after statement closed
- MySQLNonTransientConnectionException: No operations allowed after connection closed.
- nutz连接mysql异常处理:MySQLNonTransientConnectionException: No operations allowed after connection closed
- 连接mysql异常处理:MySQLNonTransientConnectionException: No operations allowed after connection closed
- No operations allowed after statement closed.
- No operations allowed after statement closed.
- com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connec
- 记weblogic JDBC 'No operations allowed after statement closed' 缘由
- MySQLNonTransientConnectionException: No operations allowed after co
- myBatis连接MySQL报异常:No operations allowed after connection closed.Connection was implicitly closed
- No operations allowed after connection closed异常的解决
- 异常问题:No operations allowed after connection closed.
- MyBatis异常:No operations allowed after connection closed
- http://cerc.tcs.uj.edu.pl/links/
- 远程通信框架-Hessian
- CentOS 6.4安装Fcitx4.0
- 每日英语
- Hibernate Annotation @mappedBy含义在使用onetoone 时候【外键双向关联】(十三)
- Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
- 0009 Java 线程的生命周期【基础】
- 图的广度优先搜索
- codec-wm8960
- 64位操作系统asp连接数据库
- Git push/pull/cherry-pick/merge/rebase
- 学习笔记4
- P2P 之 UDP穿透NAT的原理与实现
- 历届博客之星获奖博客分享