java.sql.SQLException: No operations allowed after

情況是這樣的,我使用tomcat5.0.28+hibernate2+mysql4,在M$的系統下沒出現過任何問題,但將系統裝到linux上以後(redhat9及as3),發現過一段時間後就沒有辦法連接上數據庫,查看日誌,內容如下:
ERROR http-8080-Processor22 net.sf.hibernate.util.JDBCExceptionReporter - Could not execute query
java.sql.SQLException: No operations allowed after connection closed.


Connection was closed due to the following exception:


** BEGIN NESTED EXCEPTION **


java.sql.SQLException
MESSAGE: Communication link failure: java.io.IOException, underlying cause: Unexpected end of input stream


** BEGIN NESTED EXCEPTION **


java.io.IOException
MESSAGE: Unexpected end of input stream


STACKTRACE:


java.io.IOException: Unexpected end of input stream
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1405)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1775)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1020)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1109)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2030)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1563)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:800)
at net.sf.hibernate.hql.QueryTranslator.iterate(QueryTranslator.java:848)
at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1601)
at net.sf.hibernate.impl.QueryImpl.iterate(QueryImpl.java:27)
省略...........


後來發現,原來是長時間不用這個連接後,JDBC會自動將連接釋放,然後就無法連接上,只有重新啓動tomcat方可解決,查找資料後,發現如下方法可以解決此問題。


使用hibernate:
<property name="connection.autoReconnect">true</property>  <!--這個是最主要的-->
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
加入以上property,可解決此問題,如果未使用hibernate等持久化管理框架,可在mysql的url中加入autoReconnect=true,這樣就可以解決。


mysql的站點上說是JDBC3.1.0-alpha及以前版本會出現此問題,推薦下載新的JDBC驅動,無所謂了,只要能解決問題,我也懶的換,至少現在運行了1天還未出現以前的錯誤,應該是算解決了,呵呵,等待更長時間的測試。
發佈了2 篇原創文章 · 獲贊 0 · 訪問量 2158
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章