下面查詢可以查到死鎖,但是注意其實(shí)查詢到的不是真正的死鎖,查詢到的只是是死鎖發(fā)生前的等待條件,真正的死鎖在10G中會(huì)被系統(tǒng)所阻止掉
成都創(chuàng)新互聯(lián)公司長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為巴彥企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè),巴彥網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
select t2.username||' '||t2.sid||' '||t2.serial#||' '||t2.logon_time||' '||t3.sql_text
from v$locked_object t1,v$session t2,v$sqltext t3
where t1.session_id=t2.sid
and t2.sql_address=t3.address
order by t2.logon_time ;
此外,從V$LOCK的BLOCK字段可以看到,BLOCK0表示有等待發(fā)生了,注意是行級(jí)等待,而在表級(jí)沒有死鎖或等待
select * from v$lock where block0 ;
1.查哪個(gè)過程被鎖\x0d\x0a查V$DB_OBJECT_CACHE視圖:SELECT*FROMV$DB_OBJECT_CACHEWHEREOWNER='過程的所屬用戶'ANDLOCKS!='0';\x0d\x0a2. 查是哪一個(gè)SID,通過SID可知道是哪個(gè)SESSION.\x0d\x0a查V$ACCESS視圖:SELECT*FROMV$ACCESSWHEREOWNER='過程的所屬用戶'ANDNAME='剛才查到的過程名';\x0d\x0a3. 查出SID和SERIAL#\x0d\x0a查V$SESSION視圖:SELECTSID,SERIAL#,PADDRFROMV$SESSIONWHERESID='剛才查到的SID'查V$PROCESS視圖:SELECTSPIDFROMV$PROCESSWHEREADDR='剛才查到的PADDR';\x0d\x0a4. 殺進(jìn)程(1).先殺ORACLE進(jìn)程:ALTERSYSTEMKILLSESSION'查出的SID,查出的SERIAL#';(2).再殺操作系統(tǒng)進(jìn)程:KILL-9剛才查出的SPID或ORAKILL剛才查出的SID剛才查出的SPID方法二:經(jīng)常在oracle的使用過程中碰到這個(gè)問題,所以也總結(jié)了一點(diǎn)解決方法:)1)查找死鎖的進(jìn)程:sqlplus"/assysdba"SELECT\x0d\x0as.username,l.\x0d\x0aOBJECT_ID\x0d\x0a,l.SESSION_ID,s.SERIAL#,l.ORACLE_USERNAME,l.OS_USER_NAME,l.PROCESSFROMV$LOCKED_OBJECTl,V$SESSIONSWHERE\x0d\x0al.SESSION_ID=\x0d\x0aS.SID;2)kill掉這個(gè)死鎖的進(jìn)程:altersystemkillsession‘sid,serial#’;(其中sid=\x0d\x0al.session_id)3)如果還不能解決,\x0d\x0aselectpro.spidfromv$sessionses,v$processprowhereses.sid=XXandses.paddr=pro.addr;其中sid用死鎖的sid替換。
--?死鎖查詢語句
SELECT????bs.username?"Blocking?User",?bs.username?"DB?User",
ws.username?"Waiting?User",?bs.SID?"SID",?ws.SID?"WSID",
bs.serial#?"Serial#",?bs.sql_address?"address",
bs.sql_hash_value?"Sql?hash",?bs.program?"Blocking?App",
ws.program?"Waiting?App",?bs.machine?"Blocking?Machine",
ws.machine?"Waiting?Machine",?bs.osuser?"Blocking?OS?User",
ws.osuser?"Waiting?OS?User",?bs.serial#?"Serial#",
ws.serial#?"WSerial#",
DECODE?(wk.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?Invalidation',
'LS',?'LOG?START?OR?Switch',
'RW',?'ROW?Wait',
'SQ',?'Sequence?Number',
'TE',?'Extend?TABLE',
'TT',?'Temp?TABLE',
wk.TYPE
)?lock_type,
DECODE?(hk.lmode,
0,?'None',
1,?'NULL',
2,?'ROW-S?(SS)',
3,?'ROW-X?(SX)',
4,?'SHARE',
5,?'S/ROW-X?(SSX)',
6,?'EXCLUSIVE',
TO_CHAR?(hk.lmode)
)?mode_held,
DECODE?(wk.request,
0,?'None',
1,?'NULL',
2,?'ROW-S?(SS)',
3,?'ROW-X?(SX)',
4,?'SHARE',
5,?'S/ROW-X?(SSX)',
6,?'EXCLUSIVE',
TO_CHAR?(wk.request)
)?mode_requested,
TO_CHAR?(hk.id1)?lock_id1,?TO_CHAR?(hk.id2)?lock_id2,
DECODE
(hk.BLOCK,
0,?'NOT?Blocking',??????????/**//*?Not?blocking?any?other?processes?*/
1,?'Blocking',??????????????/**//*?This?lock?blocks?other?processes?*/
2,?'Global',???????????/**//*?This?lock?is?global,?so?we?can't?tell?*/
TO_CHAR?(hk.BLOCK)
)?blocking_others
FROM?v$lock?hk,?v$session?bs,?v$lock?wk,?v$session?ws
WHERE?hk.BLOCK?=?1
AND?hk.lmode?!=?0
AND?hk.lmode?!=?1
AND?wk.request?!=?0
AND?wk.TYPE(+)?=?hk.TYPE
AND?wk.id1(+)?=?hk.id1
AND?wk.id2(+)?=?hk.id2
AND?hk.SID?=?bs.SID(+)
AND?wk.SID?=?ws.SID(+)
AND?(bs.username?IS?NOT?NULL)
AND?(bs.username??'SYSTEM')
AND?(bs.username??'SYS')
ORDER?BY?1;
查詢發(fā)生死鎖的select語句
select?sql_text?from?v$sql?where?hash_value?in?(
select?sql_hash_value?from?v$session?where?sid?in?(select?session_id?from?v$locked_object)
)
1.可能在Oracle中可以有計(jì)時(shí)器,在頻繁操作數(shù)據(jù)庫,update,insert,delete語句,在jobs中能查看到(如圖)
2.可能在.net程序中使用到timer控件,頻繁的操作數(shù)據(jù)庫update,insert,delete語句(如圖)
3.可能在.net程序中使用到Thead(線程)頻繁的操作數(shù)據(jù)庫update,insert,delete語句(這個(gè)條為個(gè)人觀點(diǎn),還未得到證實(shí))
【注釋】:此處更新數(shù)據(jù)量都比較大,比如說update的數(shù)據(jù)庫條數(shù)可能在1000條以上,
導(dǎo)致其他操作在等待(update跟其他操作都是同一張表)
4.可能使用到事務(wù)處理(Tran),沒有提交事務(wù)(CommintTran)或者沒有回滾事務(wù)(Rollback)
【注釋】:在SqlServer數(shù)據(jù)庫里面有一個(gè)隱式事務(wù),關(guān)閉時(shí)候,每次修改插入都需要手動(dòng)提交,不然就會(huì)導(dǎo)致死鎖
-- 死鎖查詢語句
SELECT bs.username "Blocking User", bs.username "DB User",
ws.username "Waiting User", bs.SID "SID", ws.SID "WSID",
bs.serial# "Serial#", bs.sql_address "address",
bs.sql_hash_value "Sql hash", bs.program "Blocking App",
ws.program "Waiting App", bs.machine "Blocking Machine",
ws.machine "Waiting Machine", bs.osuser "Blocking OS User",
ws.osuser "Waiting OS User", bs.serial# "Serial#",
ws.serial# "WSerial#",
DECODE (wk.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 Invalidation',
'LS', 'LOG START OR Switch',
'RW', 'ROW Wait',
'SQ', 'Sequence Number',
'TE', 'Extend TABLE',
'TT', 'Temp TABLE',
wk.TYPE
) lock_type,
DECODE (hk.lmode,
0, 'None',
1, 'NULL',
2, 'ROW-S (SS)',
3, 'ROW-X (SX)',
4, 'SHARE',
5, 'S/ROW-X (SSX)',
6, 'EXCLUSIVE',
TO_CHAR (hk.lmode)
) mode_held,
DECODE (wk.request,
0, 'None',
1, 'NULL',
2, 'ROW-S (SS)',
3, 'ROW-X (SX)',
4, 'SHARE',
5, 'S/ROW-X (SSX)',
6, 'EXCLUSIVE',
TO_CHAR (wk.request)
) mode_requested,
TO_CHAR (hk.id1) lock_id1, TO_CHAR (hk.id2) lock_id2,
DECODE
(hk.BLOCK,
0, 'NOT Blocking', /**//* Not blocking any other processes */
1, 'Blocking', /**//* This lock blocks other processes */
2, 'Global', /**//* This lock is global, so we can't tell */
TO_CHAR (hk.BLOCK)
) blocking_others
FROM v$lock hk, v$session bs, v$lock wk, v$session ws
WHERE hk.BLOCK = 1
AND hk.lmode != 0
AND hk.lmode != 1
AND wk.request != 0
AND wk.TYPE(+) = hk.TYPE
AND wk.id1(+) = hk.id1
AND wk.id2(+) = hk.id2
AND hk.SID = bs.SID(+)
AND wk.SID = ws.SID(+)
AND (bs.username IS NOT NULL)
AND (bs.username 'SYSTEM')
AND (bs.username 'SYS')
ORDER BY 1;
查詢發(fā)生死鎖的select語句
select sql_text from v$sql where hash_value in (
select sql_hash_value from v$session where sid in (select session_id from v$locked_object)
)
1、?用SYS登陸TOAD
2、打開菜單Database-Monitor-SessionBrowser
3、查看所有會(huì)話,是否存在鎖定資源的情況!
4、?如果有死鎖,可以將此會(huì)話Kill掉!