OMG,到底在尋找什么..................
          (構造一個完美的J2EE系統所需要的完整知識體系)
          posts - 198,  comments - 37,  trackbacks - 0
          最近的一個項目在Hibernate使用C3P0的連接池,數據庫為Mysql。開發測試沒有問題,在運行中每個一段長的空閑時間就出現異常:
          java 代碼
          1. org.hibernate.exception.JDBCConnectionException:?could?not?execute?query ??
          2. ????at?org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74) ??
          3. ????at?org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) ??
          4. ????....... ??
          5. Caused?by:?com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException:?No?operations?allowed?after?connection?closed.Connection?was?implicitly?closed?due?to?underlying?exception/error: ??
          6. ??
          7. ??
          8. **?BEGIN?NESTED?EXCEPTION?**? ??
          9. ??
          10. com.mysql.jdbc.CommunicationsException ??
          11. MESSAGE:?Communications?link?failure?due?to?underlying?exception:? ??
          12. ??
          13. **?BEGIN?NESTED?EXCEPTION?**? ??
          14. ??
          15. java.net.SocketException ??
          16. MESSAGE:?Broken?pipe ??
          17. ??
          18. STACKTRACE: ??
          19. ??
          20. java.net.SocketException:?Broken?pipe ??
          21. ????at?java.net.SocketOutputStream.socketWrite0(Native?Method) ??
          22. ????...... ??
          23. **?END?NESTED?EXCEPTION?** ??

          查看了Mysql的文檔,以及Connector/J的文檔以及在線說明發現,出現這種異常的原因是:

          ?? Mysql服務器默認的“wait_timeout”是8小時,也就是說一個connection空閑超過8個小時,Mysql將自動斷開該connection。這就是問題的所在,在C3P0 pools中的connections如果空閑超過8小時,Mysql將其斷開,而C3P0并不知道該connection已經失效,如果這時有Client請求connection,C3P0將該失效的Connection提供給Client,將會造成上面的異常。

          解決的方法有3種:

          1. 增加wait_timeout的時間。
          2. 減少Connection pools中connection的lifetime。
          3. 測試Connection pools中connection的有效性。

          當然最好的辦法是同時綜合使用上述3種方法,下面就DBCP和C3P0分別做一說明,假設wait_timeout為默認的8小時

          DBCP增加以下配置信息:

          1. //set?to?'SELECT?1' ??
          2. validationQuery?=?"SELECT?1"??
          3. //set?to?'true' ??
          4. testWhileIdle?=?"true"? ??
          5. //some?positive?integer ??
          6. timeBetweenEvictionRunsMillis?=?3600000??
          7. //set?to?something?smaller?than?'wait_timeout' ??
          8. minEvictableIdleTimeMillis?=?18000000??
          9. //if?you?don't?mind?a?hit?for?every?getConnection(),?set?to?"true" ??
          10. testOnBorrow?=?"true"??

          C3P0增加以下配置信息:

          1. //set?to?'SELECT?1'??? ??
          2. preferredTestQuery?=?'SELECT?1'? ??
          3. ?//set?to?something?much?less?than?wait_timeout,?prevents?connections?from?going?stale ??
          4. idleConnectionTestPeriod?=?18000?? ??
          5. //set?to?something?slightly?less?than?wait_timeout,?preventing?'stale'?connections?from?being?handed?out ??
          6. maxIdleTime?=?25000? ??
          7. //if?you?can?take?the?performance?'hit',?set?to?"true" ??
          8. testConnectionOnCheckout?=?true????

          更多的配置信息大家可以查看C3P0文檔,Connector/J文檔,以及DBCP的文檔

          posted on 2006-12-25 19:01 OMG 閱讀(669) 評論(0)  編輯  收藏 所屬分類: Hibernate

          <2006年12月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          IT風云人物

          文檔

          朋友

          相冊

          經典網站

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 合山市| 集安市| 罗平县| 都昌县| 威海市| 克什克腾旗| 西丰县| 科技| 莆田市| 会泽县| 香港 | 昌图县| 铜山县| 衡山县| 镇康县| 辽阳县| 田阳县| 墨玉县| 镇原县| 涡阳县| 金乡县| 勐海县| 定西市| 桂平市| 保康县| 海伦市| 库尔勒市| 攀枝花市| 金坛市| 祁连县| 十堰市| 时尚| 中方县| 阿坝| 五大连池市| 永昌县| 渑池县| 平安县| 南涧| 兴和县| 漳平市|