錯誤 ORA-01000: maximum open cursors exceeded Exception

來源:互聯網
上載者:User

ORA-01000是開發中常見的異常。這個異常表示程式中開啟的遊標數目> 資料庫中設定的可以開啟的最大遊標數。
 
這個異常通常由2個原因導致
 
1. 資料庫中設定的maximum open cursors太小
 
2. 在 Java JDBC程式中通常是代碼中開啟的ResultSet 或 PreparedStatment 沒有被關閉。 
 
什麼是遊標
 
遊標就是看成是指向結果集的指標。可以把它看成一種資源,或者一種資料結構。 
 
 
查看系統中允許的最大遊標數和 曆史上開啟的最大遊標數
 
select max(a.value) as highest_open_cur, p.value as max_open_cur
from v$sesstat a, v$statname b, v$parameter p
where a.statistic# = b.statistic# and b.name = 'opened cursors current'  and p.name= 'open_cursors'
group by p.value;
 

HIGHEST_OPEN_CUR      MAX_OPEN_CUR
 
----------------      ------------
 
69                    300
 
 
從結果可以看出,系統允許的最大遊標數是300,  曆史上最大開啟遊標數是30, 可以得出結論300 夠用了。如果兩個數字很接近或相等,那麼就要修改最大允許遊標數了。 
 
查看時誰在適用遊標


select sum(a.value) total_cur, avg(a.value) avg_cur,
 
max(a.value) max_cur, s.username, s.machine
 
from v$sesstat a, v$statname b, v$session s
 
where a.statistic# = b.statistic# and s.sid=a.sid
 
and b.name = 'opened cursors current'
 
group by s.username, s.machine order by 1 desc
 
 
 

TOTAL_CUR  AVG_CUR    MAX_CUR    USERNAME    MACHINE
 
---------  -------    -------    --------    -------
 
114        14.25      69          A          KUL-0121
 
76          8.44        48          B          KUL-0121
 
29          1.38        18                      KUL-0121
 26          8.67        13          C          xxx\KUL-012 
 
 
 
既然我們已經可以從系統中知道遊標的適用情況,那麼下面就要fix 這個異常。
 
1. 通過設定OPEN_CURSORS, MAX_VALUE增加最大遊標數

ALTER SYSTEM SET OPEN_CURSORS = MAX_VALUE SID='*' SCOPE=BOTH; 
 
 
2. 通常這個異常是由Java程式未關閉ResultSet 或者 PreparedStatment 造成的。正確的適用方法, 總是把close()放在finally中,這樣不管有沒有發生其他異常,都會關閉ResultSet 和 PreparedStatement. 


Statement stmt = conn.createStatement();
 
try {
 
    ResultSet rs = stmt.executeQuery( "SELECT FULL_NAME FROM EMP" );
 
    try {
 
        while ( rs.next() ) {
 
            System.out.println( "Name: " + rs.getString("FULL_NAME") );
 
        }
 
    } finally {
 
        try { rs.close(); } catch (Exception ignore) { }
 
    }
 
} finally {
 
    try { stmt.close(); } catch (Exception ignore) { }
 
}

關於ORA-01000: maximum open cursors exceeded" 問題分析總結

關於Oracle遊標的問題(ORA-01000: maximum open cursors exceeded) 

Oracle ORA-01000:maximum open cursors exceeded 

ORA-01000: maximum open cursors exceeded 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.