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里的静态块中,如下:
1 public class DBUtils {
2 static Connection con;
3 //读取配置文件
4 static{
5 Properties ps = new Properties();
6 //不需要写后缀名
7 ResourceBundle rundle = ResourceBundle.getBundle("db");
8 String driver = rundle.getString("driver");
9 String url = rundle.getString("url");
10 String user = rundle.getString("username");
11 String password = rundle.getString("password");
13 try {
14 //加载数据库驱动
15 Class.forName(driver);
16 } catch (ClassNotFoundException e) {
17 // TODO Auto-generated catch block
18 e.printStackTrace();
19 }
20 try {
21 //数据库连接
22 con = DriverManager.getConnection(url, user, password);
23 } catch (SQLException e) {
24 // TODO Auto-generated catch block
25 e.printStackTrace();
26 }
28 }
29 public static Connection getconnection(){
30 return con;
31 }
导致了数据库连接只创建了一次,等我调用con.close()后,再次获取数据库连接就不存在了
当我把静态块中的内容移动到获取数据库连接的方法里(让它每次调用方法的时候就创建一次连接),终于不报错了,搞定!!!!
1 public class DBUtils {
2 static Connection con;
3 //读取配置文件
4 public static Connection getconnection(){
5 Properties ps = new Properties();
6 //不需要写后缀名
7 ResourceBundle rundle = ResourceBundle.getBundle("db");
8 String driver = rundle.getString("driver");
9 String url = rundle.getString("url");
10 String user = rundle.getString("username");
11 String password = rundle.getString("password");
13 try {
14 //加载数据库驱动
15 Class.forName(driver);
16 } catch (ClassNotFoundException e) {
17 // TODO Auto-generated catch block
18 e.printStackTrace();
19 }
20 try {
21 //数据库连接
22 con = DriverManager.getConnection(url, user, password);
23 } catch (SQLException e) {
24 // TODO Auto-generated catch block
25 e.printStackTrace();
26 }
27 return con;
28 }