JDBC,ResultSet對像多次使用后再關閉的問題


原文鏈接:https://yq.aliyun.com/wenzhang/show_111763

 

問題描述

 1 //代碼...
 2 
 3 ResultSet rs = this.conn.prepareStatement("select * from test1").executeQuery();
 4 
 5 //注sql語句不同
 6 
 7 while(rs.next()){  ...}
 8 
 9 rs = this.conn.prepareStatement("select * from test2").executeQuery();
10 
11 //注sql語句不同
12 
13 while(rs.next()){  ...}
14 
15 rs = this.conn.prepareStatement("select * from test3").executeQuery();
16 
17 //注sql語句不同
18 
19 while(rs.next()){  ...}
20 
21 rs.getStatement().close();

//疑問1:多次使用rs對象后,我在最后關閉一次,這樣有沒有問題?還是要每次用完都關閉(如上代碼要關3次rs?)

//疑問2:使用rs.getStatement().close();關閉前,有沒有必要先使用rs.close();關閉,每次都是嗎(如上代碼要關3次rs?)?

 

高手回答:

  

解決方案

  正常情況下如果使用Statement執行完一個查詢,又去執行另一個查詢時這時候第一個查詢的結果集就會被關閉,也就是說,所有的Statement的查詢對應的結果集是一個,如果調用Connection的commit()方法也會關閉結果集。可保持性就是指當ResultSet的結果被提交時,是被關閉還是不被關閉。JDBC2.0和1.0提供的都是提交后ResultSet就會被關閉。不過在JDBC3.0中,我們可以設置ResultSet是否關閉。要完成這樣的ResultSet的對象的創建,要使用的Statement的創建要具有三個參數,這個Statement的創建方式也就是,我所說的Statement的第三種創建方式。如下:Statement st=createStatement(int resultsetscrollable,int resultsetupdateable,int resultsetSetHoldability)ResultSet rs = st.excuteQuery(sqlStr);前兩個參數和兩個參數的createStatement方法中的參數是完全相同的,這里只介紹第三個參數: resultSetHoldability表示在結果集提交后結果集是否打開,取值有兩個: ResultSet.HOLD_CURSORS_OVER_COMMIT:表示修改提交時,不關閉數據庫。 ResultSet.CLOSE_CURSORS_AT_COMMIT:表示修改提交時ResultSet關閉。
解決方案二:
  所以對於你的疑問要看具體的情況,一般情況下是沒錯的,不過為了便於閱讀和理解程序,我們習慣還是最好關閉,如果是多個記錄集最好是對應多個Statement
解決方案三:
  引用每次用完都關閉的話,它會立即釋放此 ResultSet 對象的數據庫和 JDBC 資源 如果在最后關閉的話,只是釋放了最后這次查詢的ResultSet,JDBC 資源. 如果不關閉的話,就會等待該對象自動關閉時(垃圾收集)發生釋放此 ResultSet 對象的數據庫和 JDBC 資源此操作。 rs.getStatement().close();自動導致ResultSet對象無效 注意只是ResultSet對象無效,ResultSet所占用的資源可能還沒有釋放,所有最好還是調有rs.close() 不執行ResultSet的close可能會導致更多的資源泄露

 

  SUN: An application calls the method Statement.close to indicate that it has finished processing a statement. All Statement objects will be closed when the connection that created them is closed. However, it is good coding practice for applications to close statements as soon as they have finished processing them. This allows any external resources that the statement is using to be released immediately. Closing a Statement object will close and invalidate any instances of ResultSet produced by that Statement object. The resources held by the ResultSet object may not be released until garbage collection runs again, so it is a good practice to explicitly close ResultSet objects when they are no longer needed. These comments about closing Statement objects apply to PreparedStatement and CallableStatement objects as well.

疑問:

  上面這位老兄說的我有點不贊同,我們可以寫一個程序來測試一下,

 

rs=stmt.executeQuery("select * from test1 ");
rs2=stmt.executeQuery("select * from test2 ");

while(rs2.next()){
System.out.println(rs2.getString(1));
System.out.println(rs2.getString(2));}

while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}  

  前面和后面我都不寫了,假如你用這個測試的,會直接報Operation not allowed after ResultSet closed,那從這個異常我們就可以看出,在你進行操作的時候,你前一個rs已經被自動關閉了,所以說是“正常情況下如果使用Statement執行完一個查詢,又去執行另一個查詢時這時候第一個查詢的結果集就會被關閉”。
解決方案四:
  每次用完都關閉的話,它會立即釋放此 ResultSet 對象的數據庫和 JDBC 資源如果在最后關閉的話,只是釋放了最后這次查詢的ResultSet,JDBC 資源.如果不關閉的話,就會等待該對象自動關閉時(垃圾收集)發生釋放此 ResultSet 對象的數據庫和 JDBC 資源此操作。 rs.getStatement().close();自動導致ResultSet對象無效 注意只是ResultSet對象無效,ResultSet所占用的資源可能還沒有釋放,所有最好還是調有rs.close()不執行ResultSet的close可能會導致更多的資源泄露。

 SUN:An application calls the method Statement.close to indicate that it has finished processing a statement. All Statement objects will be closed when the connection that created them is closed. However, it is good coding practice for applications to close statements as soon as they have finished processing them. This allows any external resources that the statement is using to be released immediately. Closing a Statement object will close and invalidate any instances of ResultSet produced by that Statement object. The resources held by the ResultSet object may not be released until garbage collection runs again, so it is a good practice to explicitly close ResultSet objects when they are no longer needed. These comments about closing Statement objects apply to PreparedStatement and CallableStatement objects as well.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM