How to deliberately cause a deadlock?

前端 未结 2 1295
死守一世寂寞
死守一世寂寞 2020-12-30 05:56

So I\'m trying to track down what looks like a deadlock problem here. I\'ve enabled deadlock logging using DBCC TRACEON(1222,-1) and DBCC TRACEON(1204 ,-1). I\'d like to t

相关标签:
2条回答
  • 2020-12-30 06:13

    Here's some T-SQL to deliberately cause a deadlock.

    Object creation:

    CREATE TABLE dbo.DeadLockTest (col1 INT)
    INSERT dbo.DeadLockTest SELECT 1
    
    CREATE TABLE dbo.DeadLockTest2 (col1 INT)
    INSERT dbo.DeadLockTest2 SELECT 1
    

    Open up a new query window and paste this code and execute it:

    BEGIN TRAN
    UPDATE dbo.DeadLockTest SET col1 = 1
    

    Open up another new query window and paste and execute this code:

    BEGIN TRAN
    UPDATE dbo.DeadLockTest2 SET col1 = 1
    UPDATE dbo.DeadLockTest SET col1 = 1
    

    Go back to your first query window (with the first BEGIN TRAN statement) and execute this code:

    UPDATE dbo.DeadLockTest2 SET col1 = 1
    

    Voila! That's a deadlock.

    0 讨论(0)
  • 2020-12-30 06:28

    This should work:

    • Insert two records, A and B.
    • Open two transactions.
    • Update record A in the first transaction and B in the second transaction.
    • When you know for sure those updates are done:
      • Update record B in the first transaction and A in the second transaction.
    0 讨论(0)
提交回复
热议问题