3

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

我也正在尝试关闭连接但无法正常工作。

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

2 回答 2

6

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

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

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

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

于 2015-11-06T08:15:29.317 回答
0

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

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

于 2015-07-08T11:59:00.310 回答