com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862)
at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1237)
at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1232)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4131)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4100)
at dao.impl.UserLoginDaoImpl.checkLogin(UserLoginDaoImpl.java:18)
at MyServlet.LoginServlet.doPost(LoginServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

 
原因应该是这样。在对数据库操作结束后关闭连接是正确的做法,没什么大问题。至于出现:No operations allowed after connection closed。这样的问题原因只有一个,你这里和数据库的连接Connection是一个Static的,程序共享这一个Connection。所以第一 次对数据库操作没问题,当把Connection关闭后,第二次还想操作数据库时Connection肯定不存在了。(每次开启tomcat后第一次能成功,刷新之后第二次就报了错)
实验了下,当我把con.close()注释了之后,就没报这个错误了,那应该如何解决呢?
后来检查发现是我把连接写在了自己建的数据库连接工具类DBUtils里的静态块中,如下:
 public class DBUtils {
static Connection con;
//读取配置文件
static{
Properties ps = new Properties();
//不需要写后缀名
ResourceBundle rundle = ResourceBundle.getBundle("db");
String driver = rundle.getString("driver");
String url = rundle.getString("url");
String user = rundle.getString("username");
String password = rundle.getString("password"); try {
//加载数据库驱动
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//数据库连接
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static Connection getconnection(){
return con;
}
}

导致了数据库连接只创建了一次,等我调用con.close()后,再次获取数据库连接就不存在了

当我把静态块中的内容移动到获取数据库连接的方法里(让它每次调用方法的时候就创建一次连接),终于不报错了,搞定!!!!

 public class DBUtils {
static Connection con;
//读取配置文件
public static Connection getconnection(){
Properties ps = new Properties();
//不需要写后缀名
ResourceBundle rundle = ResourceBundle.getBundle("db");
String driver = rundle.getString("driver");
String url = rundle.getString("url");
String user = rundle.getString("username");
String password = rundle.getString("password"); try {
//加载数据库驱动
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//数据库连接
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
}
上一篇:JS身份证真实性校验(一)


下一篇:threadPoolExecutor的基本解析