檢視oracle死鎖程序並結束死鎖
阿新 • • 發佈:2020-10-18
檢視鎖表程序SQL語句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked_mode from v$locked_object lo, dba_objects ao, v$session sess where ao.object_id = lo.object_id and lo.session_id = sess.sid; 檢視鎖表程序SQL語句2: select * from v$session t1, v$locked_object t2 wheret1.sid = t2.SESSION_ID; 殺掉鎖表程序: 如有記錄則表示有lock,記錄下SID和serial# ,將記錄的ID替換下面的738,1429,即可解除LOCK alter system kill session '738,1429'; 用這個可以查: select s.sid,s.machine,o.object_name,l.oracle_username,l.locked_mode, 'ALTER SYSTEM KILL SESSION '''||s.sid||', '||s.serial#||''';' Command from v$locked_object l,v$session s,all_objects o wherel.session_id=s.sid and l.object_id=o.object_id 可以檢視哪臺機器哪個使用者鎖了記錄, 其中command是用來殺掉鎖住記錄的session ****************************************************************************************************************** SELECT A.OBJECT_ID, B.OBJECT_NAME, A.SESSION_ID, A.ORACLE_USERNAME, A.OS_USER_NAME, A.PROCESS, A.LOCKED_MODE FROMV$LOCKED_OBJECT A, DBA_OBJECTS B WHERE A.OBJECT_ID = B.OBJECT_ID; SELECT T2.USERNAME, T2.SID, T2.SERIAL#, T2.LOGON_TIME FROM V$LOCKED_OBJECT T1, V$SESSION T2 WHERE T1.SESSION_ID = T2.SID ORDER BY T2.LOGON_TIME; ALTER SYSTEM KILL SESSION 'sid, serial#'; ******************************************************************************************************************** session 1: C:\>sqlplus hxg/hxg SQL> select * from scott.t; A B C ---------- -------------------- -------------------- 111 aa bb 222 hello world SQL> update scott.t set b='good' where a=222; 已更新 1 行。 session 2: C:\>sqlplus scott/tiger SQL> select * from scott.t; A B C ---------- -------------------- -------------------- 111 aa bb 222 hello world SQL> update t set b='asdfds' where a=222; 掛起。。。。 session 3: C:\>sqlplus "system/*** as sysdba" SQL> select sid,serial#,username,status from v$session; SID SERIAL# USERNAME STATUS ---- ---------- ------------------------------ -------- 146 25 SYS ACTIVE 147 11 SYS INACTIVE 148 2 HXG INACTIVE 150 5 SCOTT ACTIVE 151 1 ACTIVE 154 1 ACTIVE 159 6 ACTIVE 160 1 ACTIVE 161 1 ACTIVE 162 1 ACTIVE 163 1 ACTIVE SID SERIAL# USERNAME STATUS ---- ---------- ------------------------------ -------- 164 1 ACTIVE 165 1 ACTIVE 166 1 ACTIVE 167 1 ACTIVE 168 1 ACTIVE 169 1 ACTIVE 170 1 ACTIVE 已選擇18行。 SQL> alter system kill session '148,2'; 系統已更改。 SQL> select sid,serial#,username,status from v$session; SID SERIAL# USERNAME STATUS ---- ---------- ------------------------------ -------- 146 25 SYS ACTIVE 147 11 SYS INACTIVE 148 2 HXG KILLED 150 5 SCOTT INACTIVE 151 1 ACTIVE 154 1 ACTIVE 159 6 ACTIVE 160 1 ACTIVE 161 1 ACTIVE 162 1 ACTIVE 163 1 ACTIVE SID SERIAL# USERNAME STATUS ---- ---------- ------------------------------ -------- 164 1 ACTIVE 165 1 ACTIVE 166 1 ACTIVE 167 1 ACTIVE 168 1 ACTIVE 169 1 ACTIVE 170 1 ACTIVE 已選擇18行。 SQL> select V$SESSION.sid,v$session.SERIAL#,v$process.spid, rtrim(object_type) object_type,rtrim(owner) || '.' || object_name object_name, decode(lmode, 0, 'None', 1, 'Null', 2, 'Row-S', 3, 'Row-X', 4, 'Share', 5, 'S/Row-X', 6, 'Exclusive', 'Unknown') LockMode, decode(request, 0, 'None', 1, 'Null', 2, 'Row-S', 3, 'Row-X', 4, 'Share', 5, 'S/Row-X', 6, 'Exclusive', 'Unknown') RequestMode ,ctime, block b, v$session.username,MACHINE,MODULE,ACTION, decode(A.type, 'MR', 'Media Recovery', 'RT','Redo Thread', 'UN','User Name', 'TX', 'Transaction', 'TM', 'DML', 'UL', 'PL/SQL User Lock', 'DX', 'Distributed Xaction', 'CF', 'Control File', 'IS', 'Instance State', 'FS', 'File Set', 'IR', 'Instance Recovery', 'ST', 'Disk Space Transaction', 'TS', 'Temp Segment', 'IV', 'Library Cache Invalida-tion', 'LS', 'Log Start or Switch', 'RW', 'Row Wait', 'SQ', 'Sequence Number', 'TE', 'Extend Table', 'TT', 'Temp Table', 'Unknown') LockType from (SELECT * FROM V$LOCK) A, all_objects,V$SESSION,v$process where A.sid > 6 and object_name<>'OBJ$' and A.id1 = all_objects.object_id and A.sid=v$session.sid and v$process.addr=v$session.paddr; 同樣也是通過寫sql從資料字典裡查出來。 // SELECT-SQL1 // 功能:檢查被加鎖的物件 //select obj.OWNER||'.'||obj.OBJECT_NAME as OBJ_NAME, // 物件名稱(已經被鎖住) // obj.SUBOBJECT_NAME as SUBOBJ_NAME, // 子物件名稱(已經被鎖住) // obj.OBJECT_ID as OBJ_ID, // 物件ID // obj.OBJECT_TYPE as OBJ_TYPE, // 物件型別 // lock_obj.SESSION_ID as SESSION_ID, // 會話SESSION_ID // lock_obj.ORACLE_USERNAME as ORA_USERNAME, // ORACLE系統使用者名稱稱 // lock_obj.OS_USER_NAME as OS_USERNAME, // 作業系統使用者名稱稱 // lock_obj.PROCESS as PROCESS // 程序編號 //from // ( select * from all_objects where object_id in (select object_id from v$locked_object)) obj, // v$locked_object lock_obj //where obj.object_id=lock_obj.object_id; //// SELECT-SQL2 //// 功能:檢查被加鎖的物件以及加鎖的會話資訊 //// 如果需要手工解除鎖,請對照要解鎖的物件,記下SESSION_ID,SERIAL# //// 項,然後執行下面的ALTER-SQL1 //select LOCK_INFO.OWNER||'.'||LOCK_INFO.OBJ_NAME as OBJ_NAME, // 物件名稱(已經被鎖住) // LOCK_INFO.SUBOBJ_NAME as SUBOBJ_NAME, // 子物件名稱(已經被鎖住) // SESS_INFO.MACHINE as MACHINE, // 機器名稱 // LOCK_INFO.SESSION_ID as SESSION_ID, // 會話SESSION_ID // SESS_INFO.SERIAL# as SERIAL#, // 會話SERIAL# // LOCK_INFO.ORA_USERNAME as ORA_USERNAME, // ORACLE系統使用者名稱稱 // LOCK_INFO.OS_USERNAME as OS_USERNAME, // 作業系統使用者名稱稱 // LOCK_INFO.PROCESS as PROCESS, // 程序編號 // LOCK_INFO.OBJ_ID as OBJ_ID, // 物件ID // LOCK_INFO.OBJ_TYPE as OBJ_TYPE, // 物件型別 // SESS_INFO.LOGON_TIME as LOGON_TIME, // 登入時間 // SESS_INFO.PROGRAM as PROGRAM, // 程式名稱 // SESS_INFO.STATUS as STATUS, // 會話狀態 // SESS_INFO.LOCKWAIT as LOCKWAIT, // 等待鎖 // SESS_INFO.ACTION as ACTION, // 動作 // SESS_INFO.CLIENT_INFO as CLIENT_INFO // 客戶資訊 // //from // ( // select obj.OWNER as OWNER, // obj.OBJECT_NAME as OBJ_NAME, // obj.SUBOBJECT_NAME as SUBOBJ_NAME, // obj.OBJECT_ID as OBJ_ID, // obj.OBJECT_TYPE as OBJ_TYPE, // lock_obj.SESSION_ID as SESSION_ID, // lock_obj.ORACLE_USERNAME as ORA_USERNAME, // lock_obj.OS_USER_NAME as OS_USERNAME, // lock_obj.PROCESS as PROCESS // from // ( select * from all_objects where object_id in (select object_id from v$locked_object)) obj, // v$locked_object lock_obj // where obj.object_id=lock_obj.object_id // ) LOCK_INFO, // ( // select SID, // SERIAL#, // LOCKWAIT, // STATUS, // PROGRAM, // ACTION, // CLIENT_INFO, // LOGON_TIME, // MACHINE // from v$session // ) SESS_INFO //where LOCK_INFO.SESSION_ID=SESS_INFO.SID ; //// 看清楚了 下面就可以 殺死它了。 // ALTER-SQL1 // 功能:殺死會話(SESSION_ID,SERIAL#),可以手工解除鎖 // 請手工修改SESSION_ID,SERIAL#為相應值 // 注意:本功能慎重使用,有一定的破壞性,該SQL可以斷開客戶機和伺服器的連線 //ALTER SYSTEM KILL SESSION 'SESSION_ID,SERIAL#';