Can an INSERT operation result in a deadlock?

前端 未结 3 486
说谎
说谎 2021-02-01 04:43

Assuming:

  • I am using REPEATABLE_READ or SERIALIZABLE transaction isolation (locks get retained every time I access a row)
  • We are talking about multiple th
3条回答
  •  一个人的身影
    2021-02-01 05:25

    In addition to LoztInSpace's answer, inserts may cause deadlocks even without deletes or updates presence. All you need is a unique index and a reversed operations order.

    Example in Oracle :

    create table t1 (id number);
    create unique index t1_pk on t1 (id);
    
    --thread 1 :
    insert into t1 values(1);
    --thread 2
    insert into t1 values(2);
    --thread 1 :
    insert into t1 values(2);
    --thread 2
    insert into t1 values(1);  -- deadlock !
    

提交回复
热议问题