MYSQL BUG #5020及解決方法
MYSQL BUG #5020描述:
MYSQL網站BUG公示
錯誤MESSAGE:
Connection was closed due to the following exception:
** BEGIN NESTED EXCEPTION **
java.sql.SQLException
MESSAGE: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Software caused connection abort: recv failed
STACKTRACE:
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:222)
at java.io.BufferedInputStream.read(BufferedInputStream.java:277)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1385)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1532)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1923)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2236)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1555)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:89)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:880)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:273)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1678)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1664)
出錯原因:** BEGIN NESTED EXCEPTION **
java.sql.SQLException
MESSAGE: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Software caused connection abort: recv failed
STACKTRACE:
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:222)
at java.io.BufferedInputStream.read(BufferedInputStream.java:277)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1385)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1532)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1923)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2236)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1555)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:89)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:880)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:273)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1678)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1664)
我們的系統一天24小時運行,但在夜晚的時候是沒有人連接的,因此Socket連接就超時了。早上嘗試做任何事情(比如:登錄)都將以失敗告終,除非我們關閉連接或者重新建立連接。
現在我們使用的MYSQL版本號為4.016,問題一樣。
專家建議:
[Mark Matthews],翻譯如下:
注意:我不贊成Autoreconnect功能,在以后的發行版本中,它最終會被移除。
作者說明:在MYSQL4.1以后的版本當中,沒有“wait_timeout”變量,由interactiveClient代替
參考資料:http://dev.mysql.com/doc/connector/j/en/cj-configuration-properties.html
[Kirk Wylie]:翻譯如下:
如果有一個事務在運行的話,就能得到一個特別簡單的例子:
---如果連接在autoCommit模式下,autoReconnect是安全的。
---如果連接不在autoCommit模式下,但是沒有打開的事務,autoReconnect是安全的。(因為連接可能在池在里面)
---如果連接不在autoCommit模式下,有打開的事務,這樣就會拋出異常。
這樣會解決你不在autoRecom\nnect模式下的憂慮并且會保留應用程序的功能性。
如下是我個人的做法:
(STRUTS+SPRING+HIBERNATE)
1、把mysql的變量interactiveClient由 默認的28800(8個小時)延長。并告知用戶多長時間后,重新啟動TOMCAT服務器。不過,MYSQL5.0.12版本以前的所有版本,只要你設置成 了默認語言為GBK,所有對interactive_timeout(wait_timeout)的時候如何設置,重新連接或者重新啟動MYSQL后,都 是28800,現在我使用的5.0.18后,使用GBK,沒有問題。
2、在HIBERNATE當中加入:






<property name="hibernate.connection.url">jdbc:mysql://192.168.100.111/eai?autoReconnect=true</property>
<property name="enableDeprecatedAutoreconnect">true</property>
<!-- 如下的語句是使用第三方的DBCP來建立連接池 -->
<property name="dbcp.maxActive">100</property>
<property name="dbcp.whenExhaustedAction">1</property>
<!-- 看到了沒有,這個maxWait一定要大于MYSQL默認的28800(秒) -->
<property name="dbcp.maxWait">30000</property>
<property name="dbcp.maxIdle">10</property>
<property name="dbcp.ps.maxActive">100</property>
<property name="dbcp.ps.whenExhaustedAction">1</property>
<property name="dbcp.ps.maxWait">30000</property>
<property name="dbcp.ps.maxIdle">10</property>






如上兩種方法均可,推薦大家使用第二種方法,來遮擋一下MYSQL的BUG。
如果有不對之處,請大家提出自己的見解。
posted on 2006-01-18 14:48 @家軍 閱讀(5691) 評論(0) 編輯 收藏 所屬分類: J2EE技術類