2023年8月17日 星期四

grant 產生 ORA-4021 錯誤案例

Oracle 版本: 12.1.0.2.0 , RAC

OS 版本: AIX 5.3


問題描述:

於針對 gv_$ 開頭的 view 授予權限時, session 會 hang 住,最終出現 ORA-04021 timeout occurred while waiting to lock object ,無法 grant 成功 。

SQL> grant select on gv_$instance to scott;

                 *

ERROR at line 1:

ORA-04021: timeout occurred while waiting to lock object

(此命令 hang 住,最終出現 ORA-04021 的 timeout 錯誤)


問題分析:

針對 grant select on gv_$instance to scott 語法進行 hanganalyze 與 systemstate dump 分析:

SQL> oradebug setospid 515;

SQL> oradebug hanganalyze 3;

SQL> oradebug dump systemstate 266;

SQL> oradebug hanganalyze 3;


由 trace 顯示此 grant session 遭遇到 library cache lock 的等待,並且被 sid 為 344 的 session 阻塞 :


同樣的方法 trace sid 為 344 的 session ,此 session 正在進行 virtual circuit next request 的等待 :


virtual circuit next request 是一個 idle 的等待事件,當資料庫有設定 EM Express ,使用者經由網頁開啟並登入,此時資料庫會進行如下操作來撈取相關資訊顯示在 EM Express 上 :

begin

:rept := dbms_report.get_report(:report_ref, :content, :comp); 

end;


若使用者沒有登出 Web 頁面, EM Express 的 session 就會出現 virtual circuit next request 這個 idle 的等待事件。由於此時 EM Express 尚未釋放相關的 LibraryHandle ,造成其它 session 對於 gv$instance 的操作產生了 library cache lock ,一直沒有等到 lock 釋放而出現了 ORA-04021 的錯誤。


解決方法:

將登入進 EM Express 的使用者 session kill ,或者是由 EM Express 的網頁進行登出的動作,再重新進行 grant 操作便可成功。