posts - 60,comments - 71,trackbacks - 0

              做程序離不開連接數(shù)據(jù)庫,所以一些打開,關(guān)閉數(shù)據(jù)庫是經(jīng)常要執(zhí)行的操作,打開數(shù)據(jù)庫后,在程序用完后要及時關(guān)閉數(shù)據(jù)庫連接資源,以釋放內(nèi)存,避免資源耗盡.但現(xiàn)在有一個問題,即當(dāng)我們關(guān)閉了Connection對象后,Statement,ResultSet對象是否會自動關(guān)閉問題,對于這個問題,之前我在網(wǎng)上也找了相關(guān)資料,說會自動關(guān)閉,所以一段時間以來,我都是只關(guān)閉Connection對象,而沒有關(guān)閉Statement,ResultSet對象,但程序也能正常運行,程序也沒有因為資源耗盡而崩潰,對于這一點,其實是有原因的:

              1)首先,關(guān)閉了Connection對象后,是不會自動關(guān)閉Statement,ResultSet對象的:
              
          try {
          Connection con 
          = null;
          Statement st 
          = null;
          ResultSet rs 
          = null;
          con 
          = getConnection();
          st 
          = con.createStatement();
          rs 
          = st.executeQuery(sql);
          }

          catch(Exception e) {
           System.out.println(
          "ocurr error");
          }

          finally {
                    con.close();con=null;
           
          try {
              
          con.close();
           }

           
          catch(SQLException se) {
              System.out.println(
          "ocurr close error");
           }
           
          }


          System.out.println(
          "statement object:"+st);
          System.out.println(
          "resultset object:"+rs);
          上面的代碼先獲取了連接,然后只關(guān)閉了Connection對象,而沒有關(guān)閉Statement,ResultSet對象,最后兩行代碼輸出Statement,ResultSet對象,是有結(jié)果的,表明關(guān)閉了Connection對象,而沒有關(guān)閉Statement,ResultSet對象.

          2)Statement對象將由Java垃圾收集程序自動關(guān)閉,而作為一種好的編程風(fēng)格,應(yīng)在不需要Statement對象時顯式地關(guān)閉它們,這將立即釋放DBMS資源,有助于避免潛在的內(nèi)存問題.

          3)ResultSet維護指向其當(dāng)前數(shù)據(jù)行的光標(biāo).每調(diào)用一次next方法,光標(biāo)向下移動一行.最初它位于第一行之前,因此第一次調(diào)用next將把光標(biāo)置于第一行上,使它成為當(dāng)前行.隨著每次調(diào)用next導(dǎo)致光標(biāo)向下移動一行.按照從上至下的次序獲取ResultSet行,在ResultSet對象或其父輩Statement對象關(guān)閉之前,光標(biāo)一直保持有效.

               所以在打開數(shù)據(jù)庫資源后,盡量手工關(guān)閉Connection對象和Statement,ResultSet對象,要養(yǎng)成一種良好的編程風(fēng)格.

          注:他們?nèi)咧g關(guān)閉沒有任何關(guān)聯(lián),即先關(guān)閉誰沒有任何先后順序,可以先關(guān)閉他們中的任何一個,且關(guān)閉其中的任何一個對象都不會關(guān)閉其他其他對象,但一般養(yǎng)成按關(guān)閉ResultSet,Statement,Connection的順序關(guān)閉資源.

          posted on 2008-06-06 11:02 henry1451 閱讀(6757) 評論(6)  編輯  收藏 所屬分類: Java技術(shù)

          FeedBack:
          # re: 關(guān)于關(guān)閉Connection是否會自動關(guān)閉Statement,ResultSet問題[未登錄]
          2009-03-16 12:19 | Zhang
          先關(guān)閉connection 然后再關(guān)閉ResultSet 會發(fā)生什么情況?  回復(fù)  更多評論
            
          # re: 關(guān)于關(guān)閉Connection是否會自動關(guān)閉Statement,ResultSet問題[未登錄]
          2009-05-21 13:49 |
          扯淡  回復(fù)  更多評論
            
          # re: 關(guān)于關(guān)閉Connection是否會自動關(guān)閉Statement,ResultSet問題
          2009-05-24 00:43 | Wu
          @Zhang
          先關(guān)Connection,后關(guān)Statement或ResultSet都會出錯,LZ,你那個應(yīng)該只是可以打印出他們的內(nèi)存地址,實際上他們已經(jīng)被關(guān)閉了  回復(fù)  更多評論
            
          # re: 關(guān)于關(guān)閉Connection是否會自動關(guān)閉Statement,ResultSet問題
          2009-06-03 16:46 |
          樓上正解~~~
          并且關(guān)于Statement會自動關(guān)閉ResultSet
            回復(fù)  更多評論
            
          # re: 關(guān)于關(guān)閉Connection是否會自動關(guān)閉Statement,ResultSet問題
          2009-07-07 15:20 | wee
          原來是個菜鳥。
          你最后2句只是可以打印出他們的內(nèi)存地址,實際上他們已經(jīng)被關(guān)閉了。
          不信你先close connetion,再close statement, 看看會不會報錯。  回復(fù)  更多評論
            
          # re: 關(guān)于關(guān)閉Connection是否會自動關(guān)閉Statement,ResultSet問題
          2016-05-16 20:22 | 碼農(nóng)C
          謝了, 很受用!  回復(fù)  更多評論
            
          主站蜘蛛池模板: 开阳县| 宜兰市| 驻马店市| 依兰县| 柳江县| 凉城县| 棋牌| 稷山县| 西畴县| 吉木萨尔县| 德州市| 静安区| 普兰县| 霍林郭勒市| 白城市| 嵩明县| 崇明县| 凌云县| 台湾省| 黄浦区| 峨眉山市| 青冈县| 分宜县| 民丰县| 三都| 黄浦区| 宣城市| 丽江市| 米脂县| 曲松县| 绥阳县| 仁怀市| 色达县| 中山市| 眉山市| 衢州市| 静海县| 阿克| 青神县| 荆门市| 屏边|