数据库

mysql Expected to read 4 bytes, read 0 bytes before connection was unexpectedly

位置:首页 > 数据库 > mysql教程,2018-08-14 21:35
Mysql服务器默认的“wait_timeout”是8小时,Mysql将自动断开该connection。在C3P0 pools中的connections如果空闲

Mysql服务器默认的“wait_timeout”是8小时,Mysql将自动断开该connection。在C3P0 pools中的connections如果空闲超过8小时,mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有 Client请求connection,C3P0将该失效的Connection提供给Client,将会造成异常问题。


mysql抛出异常Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost,具体如下:

The last packet successfully received from the server was 2,032,862 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
	at sun.reflect.GeneratedConstructorAccessor86.newInstance(Unknown Source)
	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:411)
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3567)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3456)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3997)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2713)
	at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5060)
	at com.mchange.v2.c3p0.impl.NewProxyConnection.setAutoCommit(NewProxyConnection.java:881)
	at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:72)
	... 71 more
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:3017)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3467)
	... 79 more


分析,出现这种异常的原因是:执行sql语句时没有得到响应,连接超时将会造成上面的异常。针对此问题的解决方案,c3p0连接池的配置文件中增加如下内容:

<property name="preferredTestQuery" value="SELECT 1"/>
<property name="idleConnectionTestPeriod" value="18000"/>
<property name="maxIdleTime" value="25000"/>
<property name="testConnectionOnCheckout" value="true"/>

TAGS:read 0 bytes before connection

猜你喜欢

NewHot