デッドロック

create table tab1(col1 int primary key,col2 int);
create table tab2(col1 int primary key,col2 int);

insert into tab1 values(1,1);
insert into tab2 values(1,1);
commit;

--tx1
update tab1 set col2 = 11 where col1 = 1;

--tx2
update tab2 set col2 = 22 where col1 = 1;

--tx2
update tab1 set col2 = 21 where col1 = 1;

--tx1
update tab2 set col2 = 12 where col1 = 1;


tx2で下記エラー発生
ORA-00060: リソース待機の間にデッドロックが検出されました。

--tx2
select * from tab1;

COL1 COL2
---------- ----------
1 1

select * from tab2;

COL1 COL2
---------- ----------
1 22

tx2のtab1に対するロックのみ取り消され、
tx2のトランザクションは残存している

tx1のトランザクションは継続

 

create table tab1(col1 int primary key,col2 int);
create table tab2(col1 int primary key,col2 int);

insert into tab1 values(1,1);
insert into tab2 values(1,1);


--tx1
start transaction;
update tab1 set col2 = 11 where col1 = 1;

--tx2
start transaction;
update tab2 set col2 = 22 where col1 = 1;

--tx2
update tab1 set col2 = 21 where col1 = 1;

--tx1
update tab2 set col2 = 12 where col1 = 1;


tx1で下記エラー発生
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

--tx1
select * from tab1;

+------+------+
| col1 | col2 |
+------+------+
| 1 | 1 |
+------+------+

select * from tab2;

+------+------+
| col1 | col2 |
+------+------+
| 1 | 1 |
+------+------+

tx1のトランザクションロールバックされる

tx2のトランザクションは継続

--デッドロックタイムアウトの確認
show deadlock_timeout;


create table tab1(col1 int primary key,col2 int);
create table tab2(col1 int primary key,col2 int);

insert into tab1 values(1,1);
insert into tab2 values(1,1);


--tx1
start transaction;
update tab1 set col2 = 11 where col1 = 1;

--tx2
start transaction;
update tab2 set col2 = 22 where col1 = 1;

--tx2
update tab1 set col2 = 21 where col1 = 1;

--tx1
update tab2 set col2 = 12 where col1 = 1;


tx1で下記エラー発生
ERROR: deadlock detected
DETAIL: Process 1187 waits for ShareLock on transaction 12521; blocked by process 1192.
Process 1192 waits for ShareLock on transaction 12520; blocked by process 1187.
HINT: See server log for query details.
CONTEXT: while updating tuple (0,1) in relation "tab2"


--tx1
select * from tab1;

ERROR: current transaction is aborted, commands ignored until end of transaction block

select * from tab2;

ERROR: current transaction is aborted, commands ignored until end of transaction block

tx1のトランザクションはアボート

tx2のトランザクションは継続

 

 


create table tab1(col1 int primary key,col2 int);
create table tab2(col1 int primary key,col2 int);

insert into tab1 values(1,1);
insert into tab2 values(1,1);


--tx1
begin transaction;
update tab1 set col2 = 11 where col1 = 1;

--tx2
begin transaction;
update tab2 set col2 = 22 where col1 = 1;

--tx2
update tab1 set col2 = 21 where col1 = 1;

--tx1
update tab2 set col2 = 12 where col1 = 1;


tx2で下記エラー発生
メッセージ 1205、レベル 13、状態 51、行 3
トランザクション (プロセス ID 107) が、ロック 個のリソースで他のプロセスとデッドロックして、このトランザクションがそのデッドロックの対象となりました。トランザクションを再実行してください。


--tx2
select * from tab1;

→待ち

select * from tab2;

→待ち


tx2のトランザクションロールバックされる

tx1のトランザクションは継続