트랜잭션 교착 상태(deadlock)

교착 상태(deadlock)은 둘 이상의 트랜잭션이 서로 맞물려 상대방의 잠금이 해제되기를 기다리는 상태이다. 이러한 교착 상태에서는 서로가 상대방의 작업 수행을 차단하기 때문에 CUBRID는 트랜잭션 중 하나를 롤백시켜 교착 상태를 해결한다. 롤백되는 트랜잭션은 일반적으로 가장 적은 갱신을 수행한 것인데 보통 가장 최근에 시작된 트랜잭션이다. 시스템에 의해 트랜잭션이 롤백되자마자 그 트랜잭션이 가지고 있던 잠금이 해제되고 교착 상태에 있던 다른 트랜잭션이 진행되도록 허가된다.

이러한 교착 상태가 발생하는 상황은 예측 불가능하지만, 가급적 발생 상황을 줄이기 위해서 인덱스를 설정하여 잠금이 설정되는 범위를 줄이거나, 트랜잭션을 짧게 만들거나, 트랜잭션 격리 수준(isolation level)을 낮게 설정하는 것이 좋다.

예제

session 1

session 2

;autocommit off

AUTOCOMMIT IS OFF

set transaction isolation level 6

;xr

Isolation level set to:

SERIALIZABLE

 

1 command(s) successfully processed.

;autocommit off

AUTOCOMMIT IS OFF

set transaction isolation level 6

;xr

Isolation level set to:

SERIALIZABLE

 

1 command(s) successfully processed.

CREATE TABLE lock_tbl(host_year integer, nation_code char(3));

INSERT INTO lock_tbl VALUES (2004, 'KOR');

INSERT INTO lock_tbl VALUES (2004, 'USA');

INSERT INTO lock_tbl VALUES (2004, 'GER');

INSERT INTO lock_tbl VALUES (2008, 'GER');

COMMIT;

;xr

 

6 command(s) successfully processed.

 

SELECT * FROM lock_tbl;

;xr

 

=== <Result of SELECT Command> ===

 

    host_year  nation_code

===================================

         2004  'KOR'

         2004  'USA'

         2004  'GER'

         2008  'GER'

 

4 rows selected.

 

 

SELECT * FROM lock_tbl;

;xr

 

=== <Result of SELECT Command> ===

 

    host_year  nation_code

===================================

         2004  'KOR'

         2004  'USA'

         2004  'GER'

         2008  'GER'

 

4 rows selected.

DELETE FROM lock_tbl WHERE host_year=2008;

;xr

 

/* no result until transaction 2 releases a lock

 

C:\CUBRID>cubrid lockdb demodb

*** Lock Table Dump ***

 

Object type: Class = lock_tbl.

LOCK HOLDERS:

    Tran_index =   2, Granted_mode =   S_LOCK, Count =   2, Nsubgranules =  0

 

BLOCKED LOCK HOLDERS:

    Tran_index =   1, Granted_mode =   S_LOCK, Count =   3, Nsubgranules =  0

    Blocked_mode = SIX_LOCK

    Start_waiting_at = Fri Feb 12 14:22:58 2010

    Wait_for_nsecs = -1

 

*/

 

 

INSERT INTO lock_tbl VALUES (2004, 'AUS');

;xr

 

1 rows affected.

In the command from line 1,

 

ERROR: Your transaction (index 1, dba@ 090205|4760) has been unilaterally aborted by the system.

 

 

0 command(s) successfully processed.

 

 

/*

System rolled back the transaction 1 to resolve a deadlock

 

C:\CUBRID>cubrid lockdb demodb

*** Lock Table Dump ***

 

Object type: Class = lock_tbl.

 

LOCK HOLDERS:

    Tran_index =   2, Granted_mode = SIX_LOCK, Count =   3, Nsubgranules =  0

*/