Oracle deadlock detection Query and processing

Source: Internet
Author: User
Tags dba

--Deadlock Query statement
Select    User,
          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",
          User ",
          User ", bs.serial#" serial# ",
          ws.serial# "wserial#",
          DECODE (wk. TYPE,
                  ' Media Recovery ',
                  ' Redo Thread ',
                  ' USER Name ',
                  ' Transaction ',
                  ' DML ',
                  ' PL/SQL USER LOCK ',
                  ' Distributed Xaction ',
                  ' Control FILE ',
                  ' Instance state ',
                  ' FILE SET ',
                  ' Instance Recovery ',
                  ' Disk SPACE Transaction ',
                  ' Temp Segment ',
                  ' Library Cache invalidation ',
                  ' LOG START OR Switch ',
                  ' ROW Wait ',
                  ' Sequence number ',
                  ' Extend TABLE ',
                  ' Temp TABLE ',
                  Wk. TYPE
                 ) Lock_type,
          DECODE (Hk.lmode,
                  ' None ',
                  ' NULL ',
                  ' Row-s (SS) ',
                  ' Row-x (SX) ',
                  ' SHARE ',
                  ' S/row-x (SSX) ',
                  ' EXCLUSIVE ',
                  To_char (Hk.lmode)
                 ) Mode_held,
          DECODE (Wk.request,
                  ' None ',
                  ' NULL ',
                  ' Row-s (SS) ',
                  ' Row-x (SX) ',
                  ' SHARE ',
                  ' S/row-x (SSX) ',
                  ' EXCLUSIVE ',
                  To_char (Wk.request)
                 ) mode_requested,
          To_char (HK.ID1) lock_id1, To_char (HK.ID2) Lock_id2,
          DECODE
             (HK. BLOCK,
              ' Not Blocking ',/**//* not Blocking any other          processes * /
              ' Blocking ',              /**//* This lock blocks other processes * /
              ' Global ',           /**//* This lock are 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 (+)
      NULL)
      ' SYSTEM ')
      ' SYS ')
by 1;

Query the SELECT statement that a deadlock occurred

From v$ in(
    From v$ in(fromv$locked_object) 
)

How to check database deadlock

First, the phenomenon of database deadlock
During the execution of the program, click OK or save button, the program is not responding, there is no error.


Second, the principle of deadlock
When you perform an update or delete operation on a column of a table in a database, the statement does not mention
The other statement that the update operation for this column of data will be in the waiting state at execution time,
At this point the phenomenon is that this statement has been executing, but has not executed successfully, and no error.


Three, the method of locating the deadlock
By checking the database tables, it is possible to check which statement is deadlocked and which is the machine that generated the deadlock.


1) Execute the following statement with the DBA user

From v$
In (fromv$locked_object)

If there is a result of the output, then there is a deadlock, and the machine that can see the deadlock is which one. Field Description:
Username: The database user used by the deadlock statement;
Lockwait: The state of the deadlock if there is a content representation being deadlocked.
Status: State, active indicates deadlock
Machine: The device where the deadlock statement resides.
Program: The main application that generated the deadlock statement.


2) You can view the deadlock statement by executing the following statement with the DBA user
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))

Iv. methods of deadlock resolution
In general, as long as the statement that produces the deadlock is committed, but in the actual execution. Users can
It is not known which sentence to generate a deadlock. You can shut down the program and restart it.
Often encountered this problem in the use of Oracle, so also summed up a little solution.

  

1) To find the deadlock process:

Sqlplus "/Assysdba" (Sys/change_on_install)
From V$locked_object l,v$
WHERE L.session_id=s.sid;

2) Kill the deadlock process:

Alter system kill session ' sid,serial# '; (where sid=l.session_id)

3) If it is not resolved:

Select Pro.spid from v$session ses, v$process Pro where ses.sid=xx and ses.paddr=pro.addr;

Where SID is replaced with a dead-lock SID:

Exit
Ps-ef|grep spid

Where SPID is the process number of this process, kill the Oracle process.

Transferred from: http://sungang-82.iteye.com/blog/310781

Oracle deadlock detection Query and processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.