No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:

Karan picture Karan · May 17, 2015 · Viewed 8.8k times · Source

I got this exception repeatedly. I never close connection in my project. So why this exception?

Also I am trying to close connection but not working.

    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:

    ** BEGIN NESTED EXCEPTION **

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException MESSAGE: Communications link failure

    Last packet sent to the server was 4975 ms ago.

    STACKTRACE:

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    Last packet sent to the server was 4975 ms ago. at 
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at 
java.lang.reflect.Constructor.newInstance(Constructor.java:526) at 
com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at 
com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at 
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2985) at 
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871) at 
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414) at 
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) at 
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) at 
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536) at 
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465) at 
com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383) at 
com.erp.Beans.MarketingDashBoardBean.getMonthDiff(MarketingDashBoardBean.java:50) at
org.apache.jsp.MarketingDashBoardPage_jsp._jspService(MarketingDashBoardPage_jsp.java:586) at 
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at 
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433) 
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) 
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) at 
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) at 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) at 
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at
 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) at 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405) at 
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964) at 
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515) at 
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at 
java.lang.Thread.run(Thread.java:745) 

Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. at
 com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431) at 
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2882) ... 31 more

My Code is as below

int sr=1;

String comid=session.getAttribute("CompanyMasterId").toString();
rs=mkd.getCompanyList(CompanyMasterId,code);
System.out.println("2");
while(rs.next()){   
    rs.getString("ProspCustName");
}

and getCompanyList method is as below

public ResultSet getCompanyList(String CompanyMasterId,String code) throws Exception{
    try{
        con=ConnectionProvider.getCon();
        String sqlList="select ProspCustName,ContactPerson,Address,City from "+CompanyMasterId+"followup where MarketingRepcode='"+code+"' AND ProspCustName <> '-'  Group By ProspCustName";
        ps=con.prepareStatement(sqlList);
        rs=ps.executeQuery(sqlList);

    }catch(Exception e){
    }
    System.out.println("here");
    return rs;
}

Answer

petre picture petre · Nov 6, 2015

I had the same error. I have solve it by appending ?autoReconnect=true to the database connection link.

<property name="hibernate.connection.url">
    jdbc:mysql://your_server/your_database?autoReconnect=true
</property>

MySQL's JDBC drivers usually close a connection that remains idle for a certain amount of time (normally eight hours).

Source: https://confluence.atlassian.com/pages/viewpage.action?pageId=104300792