In the servlet I am trying to connect to the database and execute the query. An error occurs. The servlet is initialized, the connection is made, but the error: No operations allowed after connection closed, says that you can not perform operations when the connection is closed, but obviously the connection is not closed. At removal of closings of connections from a servlet and a class of connection to a DB the same error.

package servlets; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import my.service.db.MySqlConnector; public class DataFromDb extends HttpServlet { protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { System.out.println("DataFromDb service()"); Connection conn=null; try { conn = MySqlConnector.getConnection(); System.out.println(conn); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("SELECT * FROM dataonrussian");; while(rs.next()){ System.out.println(rs.getInt("id")); System.out.println(rs.getString("familia")); System.out.println(rs.getString("name")); } } catch (SQLException e) { System.out.println("SQL Exception"); e.printStackTrace(); } finally{ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } 

The TomCat server error code is as follows:

 DataFromDb service() Connect com.mysql.jdbc.JDBC4Connection@3650710f SQL Exception 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:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920) at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1304) at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1296) at com.mysql.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:2673) at com.mysql.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:2655) at servlets.DataFromDb.service(DataFromDb.java:24) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 

The code for connecting to the database:

 package my.service.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MySqlConnector { public static Connection getConnection(){ Connection conn = null; String path ="com.mysql.jdbc.Driver"; String url = "jdbc: mysql://localhost/db"; String user = "root"; String password = "12"; try { Class.forName(path); conn = DriverManager.getConnection(url, user, password); System.out.println("Connect"); } catch (ClassNotFoundException e) { System.out.println("Driver not found"); e.printStackTrace(); } catch (SQLException e) { System.out.println("Error connection"); e.printStackTrace(); } finally{ try { conn.close(); }catch (SQLException e) { e.printStackTrace(); } } return conn; } } 
  • and the class code MySqlConnector where? - jMind pm
  • added to the main code. - JAVAvladuxa
  • one
    So why do you have a finally block in MySqlConnector.getConnection () that closes a newly opened connection? - Sergey V

1 answer 1

Well, you close the connection yourself in the MySqlConnector.getConnection() method before returning it. You do not see this? Let's throw out all the "extra":

 public Connection getConnection() { try { conn = DriverManager.getConnection(url, user, password); } finally { try { conn.close(); } } return conn; }