com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientC

2019-08-17 12:54发布

问题:

This question already has an answer here:

  • No operations allowed after connection closed MYSQL 2 answers

mysql server is keep giving this below 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:425)
        at com.mysql.jdbc.Util.getInstance(Util.java:408)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
        at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1187)
        at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1182)
        at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4071)
        at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4040)
        at com.iseva.Servlet.DS_status_search.doGet(DS_status_search.java:47)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1519)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1475)
        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)
    Nov 30, 2017 2:32:30 PM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/i_seva] has started
    Nov 30, 2017 2:32:30 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
    WARNING: The web application [i_seva] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
    Nov 30, 2017 2:32:30 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
    WARNING: The web application [i_seva] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
     java.lang.Object.wait(Native Method)
     java.lang.ref.ReferenceQueue.remove(Unknown Source)
     com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:64)
     java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
     java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
     java.lang.Thread.run(Unknown Source)
    Nov 30, 2017 2:32:31 PM org.apache.catalina.loader.WebappClassLoaderBase checkStateForResourceLoading
    INFO: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
    java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
        at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1353)
        at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1029)
        at com.mysql.jdbc.AbandonedConnectionCleanupThread.checkContextClassLoaders(AbandonedConnectionCleanupThread.java:90)
        at com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:63)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

    Nov 30, 2017 2:32:32 PM org.apache.jasper.servlet.TldScanner scanJars
    INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Nov 30, 2017 2:32:32 PM org.apache.catalina.core.StandardContext reload
    INFO: Reloading Context with name [/i_seva] is completed

when try to access database in regular intervals, i am not understanding this problem , so i searched it and came to know that it can be resolved using ping how to do ping using jdbc and my connection class looks like this

 package Connectionfactory;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionFactory {


     static Connection cn;

         static
         {

             try{

                Class.forName("com.mysql.jdbc.Driver");

                } 

             catch(Exception e)
             {

                 e.printStackTrace();
             }

         }

         public static Connection getConnection()
            {


                try   
                {
                    cn=DriverManager.getConnection("jdbc:mysql://mydb.cn89qwuvudeu.us-east-2.rds.amazonaws.com:3306","XXX","XXXXXXXX");
                }

                catch(Exception p) 

                {
                    p.printStackTrace(); 
                }




             return cn; 
        }


}

please help me with this do need to add any method in above ConnectionFactory or is there any other method to solve it.

回答1:

it could be helpful to you to make the connection

package Connectionfactory;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionFactory {
     private Connection cn;
     public ConnectionFactory () {
       super();
     }
     public Connection getConnection()throws ClassNotFoundException, SQLException{
          Class.forName("com.mysql.jdbc.Driver");
          cn=DriverManager.getConnection("jdbc:mysql://mydb.cn89qwuvudeu.us-east-2.rds.amazonaws.com:3306","root","pwd");
          return cn;
     }
}


标签: java mysql jdbc