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

java.sql.SQLException: Field ‘id‘ doesn‘t have a default value
java.sql.SQLException: Field ‘id‘ doesn‘t have a default value
Java.sql.SQLException: Illegal operation on empty result set.
Java.sql.SQLException: Illegal operation on empty result set.
java.sql.SQLException: Parameter index out of range (6 >
java.sql.SQLException: Parameter index out of range (6 >
Cause: java.sql.SQLException: Unknown initial character set index ‘255‘ received from server. Initia
连接数据库的时候遇到了如下错误
【异常】Cause: java.sql.SQLException: Invalid value for getInt()
java.sql.SQLException: Invalid value for getInt()
使用QueryRunner报错:java.sql.SQLException: Cannot create
使用QueryRunner报错:java.sql.SQLException: Cannot create
Error querying database. Cause: java.sql.SQLNonTransientConnectionException:报错解决
Error querying database. Cause: java.sql.SQLNonTransientConnectionException:报错解决
Caused by: java.sql.SQLException: No value specified for parameter 1的解决过程
Caused by: java.sql.SQLException: No value specified for parameter 1的解决过程
java.sql.SQLException: connection holder is null Cause: java.sql.SQLException: connection holder is null ; uncategorized SQLEx...