Wednesday, October 31, 2007

SQL:Deadlock Graph

Batch 1:

BEGIN TRAN
USE AdventureWorks
UPDATE Person.Address
SET AddressLine2='Lockccc'
WHERE AddressID='1'
WAITFOR DELAY '0:0:5'
SELECT * FROM Person.Address
WHERE AddressID='2'
COMMIT TRAN


Batch 2:


BEGIN TRAN
USE AdventureWorks
UPDATE Person.Address
SET AddressLine2='Lockvvv'
WHERE AddressID='2'
WAITFOR DELAY '0:0:5'
SELECT * FROM Person.Address
WHERE AddressID='1'
COMMIT TRAN



Create a trace with LOCK:DeadlockGraph selected in SQL Server Profiler and run it.



Execute the Process 1 and Process 2,

You must change the value of the UPDATE statement. If you don't and re-run both batches, no action occurs in SQL Server. No deadlock happens.