连接关闭后不允许任何操作。由于基础异常/错误,连接被隐式关闭:

时间:2015-05-17 10:54:11

标签: java mysql exception connection-close

我反复得到这个例外。我从不在我的项目中关闭连接。为什么这个例外呢?

此外,我正在尝试关闭连接但不工作。

    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

我的代码如下

int sr=1;

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

和getCompanyList方法如下

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;
}

2 个答案:

答案 0 :(得分:5)

我有同样的错误。我通过将?autoReconnect=true附加到数据库连接链接来解决它。

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

MySQL的JDBC驱动程序通常关闭一段时间(通常为8小时)闲置的连接。

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

答案 1 :(得分:0)

由于您与MySQL服务器的连接丢失而发生此错误。

因为很长一段时间你的服务器没有收到任何请求。 并且您没有明确关闭连接。