[664] In the DB Oracle written interview, the simulated scene Deadlock.

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lihuarongaini/article/details/102656339

640?wx_fmt=gif

Topic section

In Oracle, a deadlock scenario simulation generated.

     

Part of the answer

Oracle deadlock in more complex, causes a deadlock there are many, there was an interviewer asks for an oral interview staff produced a deadlock simulated scene.

The following gives a simulation result in deadlock update each other based on the transaction:

1 , to create a simple two tables A and B , each table contains only one field ID , where the experimental environment for the cluster.

1SYS@RACLHR2> CREATE TABLE A (ID INT);
2Table created.
3SYS@RACLHR2> CREATE TABLE B (ID INT);
4Table created.

2 , only one is inserted into each data table, A table insertion . 1 , B Table insert 2 .

 1SYS@RACLHR2> INSERT INTO A VALUES (1);
 21 row created.
 3SYS@RACLHR2> INSERT INTO B VALUES (2);
 41 row created.
 5SYS@RACLHR2> COMMIT;
 6Commit complete.
 7SYS@RACLHR2> SELECT * FROM A;
 8        ID
 9----------
10         1
11SYS@RACLHR2> SELECT * FROM B;
12        ID
13----------
14         2

3 , in the first session SESSION1 update Table A records " 1 " to " 10000 ", and does not submit; in the second session SESSION2 update table B record " 2 " to " 20000 ", and does not submit.

SESSION1 case as follows:

 1SYS@RACLHR2> SELECT A.SID,
 2  2         B.SERIAL# ,
 3  3         C.SPID
 4  4  FROM   V$MYSTAT  A,
 5  5         V$SESSION B ,
 6  6         V$PROCESS C
 7  7  WHERE  A.SID = B.SID
 8  8  AND B.PADDR=C.ADDR
 9  9  AND    ROWNUM = 1;
10       SID    SERIAL# SPID
11---------- ---------- ------------------------
12       133          3 20906088
13SYS@RACLHR2>  UPDATE A SET ID = 10000 WHERE ID = 1;
141 row updated.

SESSION2 conditions are as follows :

 1SYS@RACLHR2> SELECT A.SID,
 2  2         B.SERIAL# ,
 3  3         C.SPID
 4  4  FROM   V$MYSTAT  A,
 5  5         V$SESSION B ,
 6  6         V$PROCESS C
 7  7  WHERE  A.SID = B.SID
 8  8  AND B.PADDR=C.ADDR
 9  9  AND    ROWNUM = 1;
10       SID    SERIAL# SPID
11---------- ---------- ------------------------
12       195         21 11010172
13SYS@RACLHR2> UPDATE B SET ID = 20000 WHERE ID = 2;
141 row updated.

At this time, the implementation process without any problems. Now then do the following, first return to the session SESSION1 , update table B records, the session appears blocked, the update can not continue:

1SYS@RACLHR2> UPDATE B SET ID = 10000 WHERE ID = 2;

Here there was a lock wait (blocking) phenomenon, because in SESSION2 has been performed in these pieces of data over the UPDATE operation, said it had not submitted the bank added row-level locking, as follows:

 1SYS@RACLHR2> SET LINE 9999
 2SYS@RACLHR2> SELECT A.INST_ID,
 3  2         A.SID,
 4  3         A.SERIAL#,
 5  4         A.SQL_ID,
 6  5         A.BLOCKING_INSTANCE,
 7  6         A.BLOCKING_SESSION,
 8  7         A.EVENT
 9  8    FROM GV$SESSION A
10  9   WHERE A.USERNAME IS NOT NULL
11 10     AND A.STATUS = 'ACTIVE'
12 11     AND A.BLOCKING_SESSION IS NOT NULL ;
13   INST_ID        SID    SERIAL# SQL_ID        BLOCKING_INSTANCE BLOCKING_SESSION EVENT
14---------- ---------- ---------- ------------- ----------------- ---------------- ------------------------------
15         2        133          3 6k793mj0duubw                 2              195 enq: TX - row lock contention

By V $ SESSION see view, Example 2 of the 195 blocks the Example 2 of the 133 sessions, i.e. in this experiment SESSION2 blocked SESSION1 .

4 , followed by a re-implementation of SQL , the deadlock will be produced. In SESSION2 , the update table A records.

1SYS@RACLHR2> UPDATE A SET ID = 10000 WHERE ID = 1;

Since SESSION1 did not submit, therefore, SESSION2 here is to wait long conversation that is HANG live, but SESSION2 there has been a deadlock occurred, this time to the first session SESSION1 can see the original has been waiting for the SQL statement is reported as follows error:

1SYS@RACLHR2> UPDATE B SET ID = 10000 WHERE ID = 2;
2UPDATE B set id = 10000 WHERE id = 2
3       *
4ERROR at line 1:
5ORA-00060: deadlock detected while waiting for resource

At this time, if the query V $ SESSION view, it can be seen that Example 2 is 133 blocks the Example 2 of the 195 sessions, i.e. in this experiment SESSION1 blocked session2 , and just opposite the obstruction described Oracle done automatically processed:

 1SYS@RACLHR2> SET LINE 9999
 2SYS@RACLHR2> SELECT A.INST_ID,
 3  2         A.SID,
 4  3         A.SERIAL#,
 5  4         A.SQL_ID,
 6  5         A.BLOCKING_INSTANCE,
 7  6         A.BLOCKING_SESSION,
 8  7         A.EVENT
 9  8    FROM GV$SESSION A
10  9   WHERE A.USERNAME IS NOT NULL
11 10     AND A.STATUS = 'ACTIVE'
12 11      AND A.BLOCKING_SESSION IS NOT NULL ;
13   INST_ID        SID    SERIAL# SQL_ID        BLOCKING_INSTANCE BLOCKING_SESSION EVENT
14---------- ---------- ---------- ------------- ----------------- ---------------- ------------------------------
15         2        195         21 5q7t3877fdu3n                 2              133 enq: TX - row lock contention

& Description: 

About row-level deadlock more details you can refer to my BLOG : http://blog.itpub.net/26736162/viewspace-1744705/ , http://blog.itpub.net/26736162/viewspace-1744719/

About ITL more deadlock, please refer to: http://blog.itpub.net/26736162/viewspace-2124771/

For line-level analog reference deadlock: http://blog.itpub.net/26736162/viewspace-212 7247 /

This article taken from "Oracle programmer written interview book", author: wheat seedlings

640?wx_fmt=gif

--------------- quality wheat class ------------

640?wx_fmt=png

Details can add wheat teacher QQ micro-channel or private chat.

640?wx_fmt=gif

About Me: Wheat seedlings

author: Wheat seedlings, only focus on database technology, pay more attention to the use of technology 

● On the blog address: http://blog.itpub.net/26736162/abstract/1/

 The series title comes from the author's study notes, some sort from the network, if at tort or inappropriate please understand

Copyright, please share this article, reproduced Please keep the source 

  QQ: 646 634 621 QQ group: 618 766 405

 provide OCP, OCM some of the most highly available and practical skills training

●  subject to answer if inappropriate, but also hope our friends criticism and common progress

640?wx_fmt=gifDBA Collection

FIG long press identification code or a two-dimensional micro-channel scanning to follow the two-dimensional code in FIG wheat seedlings micro-channel public number: xiaomaimiaolhr, learn the most practical database technology.

640?wx_fmt=gif

640?wx_fmt=gif

640?wx_fmt=png

Guess you like

Origin blog.csdn.net/lihuarongaini/article/details/102656339