我的Blog我做主^_^

          走向一條通往JAVA的不歸路...

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            64 隨筆 :: 68 文章 :: 77 評(píng)論 :: 0 Trackbacks

          前一段時(shí)間朋友用hibernate+mysql整了一個(gè)應(yīng)用,出現(xiàn)tomcat放一夜,mysql連接出現(xiàn)錯(cuò)誤的情況,具體的錯(cuò)誤信息忘記了。

          在網(wǎng)上查找一下,找到了這個(gè)帖子,還有就是這個(gè)了;原來Mysql在經(jīng)過8小時(shí)不使用后會(huì)自動(dòng)關(guān)閉已打開的連接,摘錄原文如下:

          5.4.

          I have a servlet/application that works fine for a day, and then stops working overnight

          MySQL closes connections after 8 hours of inactivity. You either need to use a connection pool that handles stale connections or use the "autoReconnect" parameter (see "Developing Applications with MySQL Connector/J").

          Also, you should be catching SQLExceptions in your application and dealing with them, rather than propagating them all the way until your application exits, this is just good programming practice. MySQL Connector/J will set the SQLState (see java.sql.SQLException.getSQLState() in your APIDOCS) to "08S01" when it encounters network-connectivity issues during the processing of a query. Your application code should then attempt to re-connect to MySQL at this point.

          現(xiàn)把具體方法貼出來,以供大家共享.
          方法一:
                         設(shè)置你的MYSQL數(shù)據(jù)庫:wait_timeout=24*60*60<秒>,把它的值設(shè)置大一點(diǎn),呵呵
           方法二:
                         配置Hibernate C3p0連接池,配置如下:
          <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
             <property name="c3p0.max_size">20</property>
             <property name="c3p0.min_size">5</property>
             <property name="c3p0.timeout">20</property>
             <property name="c3p0.max_statements">100</property>
             <property name="c3p0.idle_test_period">120</property>
             <property name="c3p0.acquire_increment">2</property>

          注意這里標(biāo)紅的部分,要設(shè)置c3p0.timeout的值小于MySql的wait_timeout的值,這樣才行,要不還會(huì)出現(xiàn)異常.

          這次是一個(gè)教訓(xùn),所以不論從穩(wěn)定還是性能的考慮,都應(yīng)該選擇相對(duì)更加成熟的連接池。



          posted on 2007-05-23 10:52 java_蟈蟈 閱讀(957) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 凉山| 南京市| 湖南省| 乐安县| 崇州市| 乐平市| 长治市| 汨罗市| 治多县| 磐安县| 汕头市| 峨边| 抚顺县| 柳江县| 且末县| 景谷| 营山县| 信丰县| 宜丰县| 珲春市| 平果县| 百色市| 泸溪县| 茂名市| 陈巴尔虎旗| 晋江市| 巴楚县| 隆回县| 游戏| 浮梁县| 紫阳县| 额尔古纳市| 青河县| 尚志市| 五指山市| 加查县| 邢台市| 武邑县| 当涂县| 温州市| 延吉市|