使用DBMS_APPLICATION_INFO.SET_CLIENT_INFO來定位當前session所在的程式模組

swq618發表於2016-10-24
問題提出:
在B/S系統中,頁面紛繁複雜,而且經過系統長時間執行,如果發現某個session的wait比較嚴重,進行sql最佳化時,很難知道這個sql是用在程式的哪個模組,很多時候只能根據開發人員的經驗來定位這個sql,或者開啟程式程式碼,搜尋。這樣的效率很低,這裡介紹一個方法來快速定位發生問題的sql的位置。[@more@]

ORACLE提供了DBMS_APPLICATION_INFO包,其中提供了set_client_info、set_module、set_action、read_module、read_client_info、set_session_longops幾個過程,其中分別用於設定客戶端資訊、設定module資訊(這裡同時必須設定一個action資訊)、設定action資訊、讀取module資訊(同時也會得到action資訊)、讀取client_info資訊和手動設定一個會話為longops。
下面來看一個示例:
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.2.0
Connected as SYS

SQL> select distinct sid from v$mystat;

SID
----------
1036

SQL> select module,action,client_info from v$session where sid=1036;

MODULE ACTION CLIENT_INFO
------------------------------------------------ -------------------------------- ----------------------------------------------------------------
PL/SQL Developer Command Window - New
可以看到CLIENT_INFO為空。
SQL> exec dbms_application_info.set_client_info('test client info');

PL/SQL procedure successfully completed

SQL> select module,action,client_info from v$session where sid=1036;

MODULE ACTION CLIENT_INFO
------------------------------------------------ -------------------------------- ----------------------------------------------------------------
PL/SQL Developer Command Window - New test client info

設定後的CLIENT_INFO
SQL> exec dbms_application_info.set_module(module_name => 'test module',action_name => 'test action');

PL/SQL procedure successfully completed

SQL> select module,action,client_info from v$session where sid=1036;

MODULE ACTION CLIENT_INFO
------------------------------------------------ -------------------------------- ----------------------------------------------------------------
test module test action test client info

設定後的MODULE和ACTION

詳細的解釋請參考oracle的說明,在oracle的package的說明中就有詳細的解釋。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29674916/viewspace-2126986/,如需轉載,請註明出處,否則將追究法律責任。

相關文章