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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM