mysql Can not read response from server. Expected to read 4 bytes 异常



### Error querying database.  Cause: java.sql.SQLException: Could not retrieve transation read-only status server
### The error may exist in sqlmaps/order/ExternalOrder.xml
### The error may involve ExternalOrder.findExternalOrderByOrderId-Inline
### The error occurred while setting parameters
### Cause: java.sql.SQLException: Could not retrieve transation read-only status server
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        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 com.taxi.filter.RequestHandleFilter.doFilter(RequestHandleFilter.java:97)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at com.taxi.filter.SignatureProvingFilter.doFilter(SignatureProvingFilter.java:111)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at com.taxi.filter.AuthorizeFilter.doFilter(AuthorizeFilter.java:145)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
        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:1009)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1686)
        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:722)
Caused by: org.springframework.dao.TransientDataAccessResourceException:


### Error querying database.  Cause: java.sql.SQLException: Could not retrieve transation read-only status server
### The error may exist in sqlmaps/order/ExternalOrder.xml
### The error may involve ExternalOrder.findExternalOrderByOrderId-Inline
### Cause: java.sql.SQLException: Could not retrieve transation read-only status server
        at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:107)
        at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:71)
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364)
        at com.sun.proxy.$Proxy12.selectOne(Unknown Source)
        at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159)
        at com.newtaxi.biz.common.api.impl.ExternalOrderDaoImpl.findExternalOrderByOrderId(ExternalOrderDaoImpl.java:80)
        at com.newtaxi.biz.api.order.impl.ExternalOrderServiceImpl.findExternalOrderByOrderId(ExternalOrderServiceImpl.java:20)
        at com.taxi.order.rpc.PassengerReadObtainOrderDriverRPC.run(PassengerReadObtainOrderDriverRPC.java:95)
        at com.taxi.action.PassengerAction.doService(PassengerAction.java:38)
        at com.taxi.web.controller.PassengerController.run(PassengerController.java:55)
        at com.taxi.web.controller.PassengerController.run(PassengerController.java:48)
        at sun.reflect.GeneratedMethodAccessor215.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        ... 35 more
Caused by: java.sql.SQLException: Could not retrieve transation read-only status server
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:951)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
        at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3936)
        at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3907)
        at com.mysql.jdbc.PreparedStatement.checkReadOnlySafeStatement(PreparedStatement.java:1258)
        at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1278)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.execute(NewProxyPreparedStatement.java:989)
        at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:56)
        at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:70)
        at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:57)
        at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:259)
        at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:132)
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105)
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:62)
        at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)


The last packet successfully received from the server was 28,806,550 milliseconds ago.  The last packet sent successfully to the ser
ver was 0 milliseconds ago.
        at sun.reflect.GeneratedConstructorAccessor296.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3670)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3559)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4110)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2758)
        at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612)
        at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3930)
        ... 70 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was une
xpectedly lost.
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3119)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3570)
        ... 78 more


线上http总共  每天有几十个到  一两百个这种异常(这个问题一直都有,,,只是平时没怎么注意),经过排查,原因是 c3p0连接池参数配置不对导致
原因:mysql会对空闲连接做回收处理,默认是8个小时,,,,    但是之前的c3p0连接池没有对idle 连接做检测处理,导致一些连接在池在里面睡的时间超过8个小时,,,,
8小时后被拿来使用,,,结果报以上异常,,,因为mysql已经把该连接关掉了,,,
 
解决办法,c3p0连接池配置加上  idleConnectionTestPeriod =1800  这个项,意思是如果连接在池里睡的时间超过1800s会做一次连接可用的检查,如果发现连接不可用,,就释放掉

再加个 jdbc.maxIdleTime=14400 (之前配置的值是30000,超过mysql的8个小时) 连接空闲4小时会自动销毁

参考文章

http://stackoverflow.com/questions/13950496/what-is-java-io-eofexception-message-can-not-read-response-from-server-expect

http://stackoverflow.com/questions/12507021/best-configuration-of-c3p0

http://www.mchange.com/projects/c3p0/index.html#preferredTestQuery

http://jony-hwong.iteye.com/blog/727647

你可能感兴趣的:(mysql)