Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
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));}
}
来自: http://blog.csdn.net/bedweather/article/details/6743951
Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed的更多相关文章
- com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after co ...
- myBatis连接MySQL报异常:No operations allowed after connection closed.Connection was implicitly closed
网站运行一个晚上,早上来上班,发现报错: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTra ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. 解决
ERROR - No operations allowed after connection closed. 2011-12-07 11:36:09 - ERROR - query failed or ...
- MySQLNonTransientConnectionException: No operations allowed after connection closed
原因分析 查看了Mysql的文档,以及Connector/J的文档以及在线说明发现,出现这种异常的原因是: Mysql服务器默认的"wait_timeout"是8小时,也就是说一个 ...
- Spring Boot连接MySQL长时间不连接后报错`com.mysql.cj.core.exceptions.ConnectionIsClosedException: No operations allowed after connection closed.`的解决办法
报错:com.mysql.cj.core.exceptions.ConnectionIsClosedException: No operations allowed after connection ...
- spring boot下mybatis遇到No operations allowed after connection closed.
在配置文件中添加 # for initial,min,maxspring.datasource.initialSize=5spring.datasource.minIdle=5spring.datas ...
- No operations allowed after connection closed--转
https://www.jianshu.com/p/1626d41572f2 Spring boot的单数据源配置比较简单,只需要在application.properties配置相关的jdbc连接的 ...
- Spring Boot环境下出现No operations allowed after connection close错误
一个基于springcloud的微服务项目,详细配置: SpringCloud + SpringMVC+SpringData JPA+ MySql+Postgresql 其中项目配置了多数据源,前期开 ...
- MySQL Binlog 【ROW】和【STATEMENT】选择(转)
前言: 二进制日记录了数据库执行更改的操作,如Insert,Update,Delete等.不包括Select等不影响数据库记录的操作,因为没有对数据进行修改.二进制主要的功能有:复制(Re ...
随机推荐
- 纯css 写三角形
<div style="width: 0;height: 0;border-left: 6px solid transparent;border-right: 6px solid tr ...
- Jersey(1.19.1) - Conditional GETs and Returning 304 (Not Modified) Responses
Conditional GETs are a great way to reduce bandwidth, and potentially server-side performance, depen ...
- xml使用系统整理
1. 认识xml 可扩展标记语言,一种用于标记电子文档使其具有结果性的标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 2. 和超文本标记语言区别 2.1 ...
- Android Wi-Fi基本操作
从用户角度看,Android Wi-Fi模块自下向上可以看为5层:硬件驱动程序,wpa_suppplicant,JNI,WiFi API,WifiSettings应用程序. 1.wpa_supplic ...
- hiho拓扑排序专题 ——第四十八、四十七周
拓扑排序·一 分析: 此题就是求一个有向图中是否存在环. 如存在环则输出"Wrong", 若不存在环, 说明课程安排的合理,输出"Correct". 题中的提示 ...
- 实现Foreach遍历
实现Foreach遍历的集合类,需要实现IEnumerable接口,泛型集合则需要实现IEnumerable<T>接口 using System; using System.Collect ...
- iOS中的动画
iOS中的动画 Core Animation Core Animation是一组非常强大的动画处理API,使用它能做出非常绚丽的动画效果,而且往往是事半功倍,使用它需要添加QuartzCore .fr ...
- 从客户端中检测到有潜在危险的 request
如题,当遇到这种情况该怎么办呢? 通常情况下一下2种解决方案就可以解决问题了: 解决方案一: 在.aspx文件头中加入这句: <%@ Page validateReques ...
- Php 操作事务
PHP来操作数据库 关于事务操作 连接数据 mysql_connect('localhost','root','123'); 设置字符集 mysql_query('set names utf8'); ...
- ViewPager的简单例子
这个例子是按照官网上的例子写的,有点抄袭的嫌疑,但是自己单独写一下会加深自己的印象. 首先是MainAcitivity.xml: <LinearLayout xmlns:android=&quo ...