java连接mysql出现异常No operations allowed after connection closed,怎么破?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java连接mysql出现异常No operations allowed after connection closed,怎么破?相关的知识,希望对你有一定的参考价值。

你在//关闭连接那里,把关闭的代码写上

rs.close();
pst.close();
conn.close();追问

加上也不行的哦!

追答

getConnection方法里面你关闭了,把关闭代码去掉

参考技术A 你的这个报错是不是一般都是第二天早上第一次访问时出现。然后第二次就好了,如果是的话,是因为:mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该 connection。当使用是重新建立connection,看情况是不是你的网站访问量应该不大。
查看服务器wait_timeout是不是28800
mysql﹥ show global variables like 'wait_timeout';
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| wait_timeout | 28800 |
+---------------+---------+
1 row in set (0.00 sec)
如果是的话把时间改长些。

数据库连接异常

连接数据库时经常出现的异常就是java.lang.ClassNotFoundException: com.mysql.jdbc.Driver。所以今天我结合自己的经验给大家总结一下,碰到这类的异常要如何处理,首先给大家分享一下我在用MyBatis逆向生成时碰到到的这个异常。

异常信息如下

java.lang.RuntimeException: Exception getting JDBC Driver

    at org.mybatis.generator.internal.JDBCConnectionFactory.getDriver(JDBCConnectionFactory.java:100)
    at org.mybatis.generator.internal.JDBCConnectionFactory.getConnection(JDBCConnectionFactory.java:70)
    at org.mybatis.generator.config.Context.getConnection(Context.java:753)
    at org.mybatis.generator.config.Context.introspectTables(Context.java:631)
    at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:257)
    at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:139)
    at com.MyBatis.Test.create(Test.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:70)
    at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.mybatis.generator.internal.ObjectFactory.internalClassForName(ObjectFactory.java:181)
    at org.mybatis.generator.internal.ObjectFactory.externalClassForName(ObjectFactory.java:136)
    at org.mybatis.generator.internal.JDBCConnectionFactory.getDriver(JDBCConnectionFactory.java:97)
    ... 28 more

异常原因:pom.xml配置文件中未添加mysql依赖
解决方法:pom.xml配置文件中标签中添加mysql依赖

解决该类异常方向

  1. 看了pom.xml文件中是否导入了 “mysql-connector-java” 这个jar包
  2. 如果pom.xml文件中含有jar包信息,检查是否已经导入jar包
  3. 查看数据库连接信息是否有细微的小错误,mysql四大参数容易出错,比如少个字母或者多个字母都不行,可参考如下:
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/**
    jdbc.username=root
    jdbc.password=root

以上是关于java连接mysql出现异常No operations allowed after connection closed,怎么破?的主要内容,如果未能解决你的问题,请参考以下文章

mysql链接出现: No operations allowed after statement closed.

java连接mysql :No Suitable Driver Found For Jdbc 解决方法

MySQL:No operations allowed after connection closed错误原因及解决

myBatis连接MySQL报异常:No operations allowed after connection closed.Connection was implicitly closed(示例代

mysql5.7.26 临时表异常记录

连接db2数据库出现No buffer space available (maximum connections reached?)