java.net.SocketException: 断开的管道 (Write failed) 错误,数据库隔一段时间就断开的问题...

一个项目使用了druid作为数据源,每过几十分钟刷新页面就会出现 java.net.SocketException: 断开的管道 (Write failed) 错误。

后来给连接池配置部分加上了 

<property name="validationQuery" value="select 1"/>
       
<property name="testOnBorrow" value="true"/>

问题得到了解决,应该是长时间没有刷新页面导致数据库连接断开了,再次访问的时候使用了断开的数据库连接,所以出现报错,加入了

testOnBorrow 属性对数据库连接的有效性进行了验证,集群某实例宕掉时,如果连接刚好不处于通信阶段,tcp连接正处于CLOSE_WAIT状态或已关闭,当应用通过连接池getConnection时,
在borrow时会检测连接,由于连接已关闭,于是报了如下报错,并重新建立新连接,此时的新连接到集群的其他实例上了。后面能正常通信。
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
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:3143)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:597)
    ... 21 more

 

转载于:https://www.cnblogs.com/wolf-zt/p/6522219.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值