码迷,mamicode.com
首页 > 数据库 > 详细

oracle 死锁查询及处理

时间:2015-05-13 19:26:51      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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;

--------------------------------------------------------------------------------
查询发生死锁的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)用dba用户执行以下语句
select username,lockwait,status,machine,program from v$session where sid in
(select session_id from v$locked_object)
如果有输出的结果,则说明有死锁,且能看到死锁的机器是哪一台。字段说明:
Username:死锁语句所用的数据库用户;
Lockwait:死锁的状态,如果有内容表示被死锁。
Status: 状态,active表示被死锁
Machine: 死锁语句所在的机器。
Program: 产生死锁的语句主要来自哪个应用程序。
2)用dba用户执行以下语句,可以查看到被死锁的语句。
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))

四、         死锁的解决方法
     一般情况下,只要将产生死锁的语句提交就可以了,但是在实际的执行过程中。用户可
能不知道产生死锁的语句是哪一句。可以将程序关闭并重新启动就可以了。
 经常在Oracle的使用过程中碰到这个问题,所以也总结了一点解决方法。

  1)查找死锁的进程:

sqlplus "/as sysdba" (sys/change_on_install)
SELECT s.username,l.OBJECT_ID,l.SESSION_ID,s.SERIAL#,
l.ORACLE_USERNAME,l.OS_USER_NAME,l.PROCESS 
FROM V$LOCKED_OBJECT l,V$SESSION S WHERE l.SESSION_ID=S.SID;

  2)kill掉这个死锁的进程:

  alter system kill session ‘sid,serial#’; (其中sid=l.session_id)

  3)如果还不能解决:

select pro.spid from v$session ses,
v$process pro where ses.sid=XX and 
ses.paddr=pro.addr;

  其中sid用死锁的sid替换:

exit
ps -ef|grep spid

  其中spid是这个进程的进程号,kill掉这个Oracle进程。

oracle 死锁查询及处理

标签:

原文地址:http://www.cnblogs.com/ymj126/p/4501050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!