java.sql.SQLException: No operations allowed after statement closed.

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: java.sql.SQLException: No operations allowed after statement closed.原因很简单:这里和数据库的连接Connection是一个Static的,程序共享这一个Connection。

java.sql.SQLException: No operations allowed after statement closed.


原因很简单:这里和数据库的连接Connection是一个Static的,程序共享这一个Connection。所以第一次对数据库操作没问题,当把Connection关闭后,第二次还想操作数据库时Connection肯定不存在了。

http://zhangsha1251.blog.163.com/blog/static/62624053201182111921783/

MyBatis连接MySQL出错:No operations allowed after connection closed

myBatis连接MySQL报异常:No operations allowed after connection closed.Connection was i

MyBatis连接MySQL出错:No operations allowed after connection closed

https://stackoverflow.com/questions/25883403/java-sql-sqlexception-no-operations-allowed-after-connection-closed

Well I found, the default time for closing connection if it has been inactive too long is 28800 seconds(8 hrs) for MYSQL (& not 34,247,052 milliseconds).

show global variables;

In MYSQL, I reset that value to 172800 seconds(48 hrs as per my requirement) using

set global wait_timeout=172800;

and it worked fine.

Thanks a lot for the help!

Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed

 Mysql的数据库连接默认等待时限(wait_timeout)是8个小时,在该时限内如果没有使用该Connection(超过了这个时限),Connection就会被关闭。虽然该Connection已经被关闭,但Connection不为空。第一次调用时没有问题的,如果长时间没有使用该Connection,Connection会被Mysql关闭(但不为null)。此时调用该Connection时就会抛出异常com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed by the driver.

  如果一个程序中使用一个共同的static的Connection时,这种问题就很容易出现。

查看Mysql的默认wait_timeout值(以下是我的Mysql中的配置)

show variables like '%timeout%';

+----------------------------+-------+

| Variable_name              | Value |

+----------------------------+-------+

| connect_timeout            | 10    |

| delayed_insert_timeout     | 300   |

| innodb_lock_wait_timeout   | 50    |

| innodb_rollback_on_timeout | OFF   |

| interactive_timeout        | 6000  |

| net_read_timeout           | 30    |

| net_write_timeout          | 60    |

| slave_net_timeout          | 3600  |

| table_lock_wait_timeout    | 50    |

| wait_timeout               | 6000  |

+----------------------------+-------+

10 rows in set (0.00 sec)

wait_timeout的值是6000秒。

解决方法:

1.修改Mysql的配置文件,添加一个属性,一旦Connection被自动关闭,便进行自动重连

2.在程序中进行判断,如hemowolf的方式。

3.使用更高级的数据库连接的jar,老的都会有这种问题。

4.不直接使用jdbc,改用Spring中的jdbcTemplate。

本文地址:https://www.jianshu.com/p/76ba02240eac

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
6月前
|
SQL 存储 Java
JDBC Statement:执行 SQL 语句的重要接口
在Java应用程序中,与数据库进行交互是一项常见的任务。为了执行数据库操作,我们需要使用JDBC(Java Database Connectivity)来建立与数据库的连接并执行SQL语句。Statement接口是JDBC中的一个重要接口,它用于执行SQL语句并与数据库进行交互。本文将详细介绍Statement接口的使用,包括如何创建Statement对象、执行SQL语句、处理结果等内容。
99 0
|
18天前
|
SQL 关系型数据库 MySQL
java.sql.SQLException: No operations allowed after statement closed.
java.sql.SQLException: No operations allowed after statement closed.
22 0
|
9月前
|
SpringCloudAlibaba Java Nacos
客户端启动报错java.lang.IllegalArgumentException: no server available的解决方案 SpringCloud中 Nacos做注册中心
客户端启动报错java.lang.IllegalArgumentException: no server available的解决方案 SpringCloud中 Nacos做注册中心
214 0
|
6月前
解决java.lang.Exception:No runnable methods问题~
解决java.lang.Exception:No runnable methods问题~
|
6月前
|
Java
已解决Java.net.MalformedURLException异常的有效方法java.net.MalformedURLException: no protocol异常处理
已解决Java.net.MalformedURLException异常的有效方法java.net.MalformedURLException: no protocol异常处理
343 0
|
3月前
|
Java
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
29 1
|
8月前
|
SQL 安全 前端开发
【JAVA基础 】- 深入理解Statement和PrepateStatement
【JAVA基础 】- 深入理解Statement和PrepateStatement
55 0
|
4月前
|
SQL 分布式计算 Hadoop
【已解决[ERROR] Could not execute SQL statement. Reason:java.lang.ClassNotFoundException: org.apache.had
【已解决[ERROR] Could not execute SQL statement. Reason:java.lang.ClassNotFoundException: org.apache.had
82 0
|
4月前
Junit报错java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=test]的解决
Junit报错java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=test]的解决
36 0
|
6月前
|
Java 开发者
【Java异常】使用通用Mapper ,报There is no getter for property named ‘distinct‘ in ‘class 错
【Java异常】使用通用Mapper ,报There is no getter for property named ‘distinct‘ in ‘class 错
55 0