Energy of Love  
          日歷
          <2009年8月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345
          統(tǒng)計
          • 隨筆 - 70
          • 文章 - 0
          • 評論 - 80
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

          最近的一個項(xiàng)目在Hibernate使用C3P0的連接池,數(shù)據(jù)庫為Mysql。開發(fā)測試沒有問題,在運(yùn)行中每個一段長的空閑時間就出現(xiàn)異常:


          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. ** BEGIN NESTED EXCEPTION **
          7. com.mysql.jdbc.CommunicationsException
          8. MESSAGE: Communications link failure due to underlying exception:
          9. ** BEGIN NESTED EXCEPTION **
          10. java.net.SocketException
          11. MESSAGE: Broken pipe
          12. STACKTRACE:
          13. java.net.SocketException: Broken pipe
          14. at java.net.SocketOutputStream.socketWrite0(Native Method)
          15. ......
          16. ** END NESTED EXCEPTION **

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

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

          解決的方法有3種:

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

          當(dāng)然最好的辦法是同時綜合使用上述3種方法,下面就DBCP和C3P0分別做一說明,假設(shè)wait_timeout為默認(rèn)的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增加以下配置信息:
           

        11. //獲取connnection時測試是否有效
        12. testConnectionOnCheckin = true
        13. //自動測試的table名稱
        14. automaticTestTable=C3P0TestTable
        15. //set to something much less than wait_timeout, prevents connections from going stale
        16. idleConnectionTestPeriod = 18000
        17. //set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out
        18. maxIdleTime = 25000
        19. //if you can take the performance 'hit', set to "true"
        20. testConnectionOnCheckout = true
        21.  

           

           

          在配置文件中要寫成  <property name="minPoolSize"><value>1</value></property> 格式

          不能寫成 這樣<property name="properties">
             <props>
              <prop key="c3p0.initialPoolSize">1</prop>

             </props>

          </property>

          c3p0不能完全識別!!

          posted on 2009-08-11 17:27 不高興 閱讀(371) 評論(0)  編輯  收藏 所屬分類: JavaSpring
           
          Copyright © 不高興 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 石景山区| 衢州市| 宝坻区| 闵行区| 昔阳县| 神池县| 蒙城县| 西昌市| 台州市| 微博| 中山市| 祁东县| 蓝田县| 灌云县| 宝兴县| 峨边| 西峡县| 崇信县| 巴林左旗| 金乡县| 楚雄市| 鄂托克旗| 依兰县| 四会市| 红河县| 福贡县| 乌苏市| 安多县| 临夏县| 西和县| 昆明市| 武宣县| 浦县| 嫩江县| 镶黄旗| 斗六市| 凤冈县| 邵武市| 加查县| 苍南县| 富平县|