Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Better understanding dead lock behaviour

$
0
0

Hello,

I'm trying to better understandt the internals of deadlocks so I did the following

CREATE TABLE [dbo].[teste](
[c1] [char](50) NOT NULL PRIMARY KEY,
[c2] [bigint] NOT NULL,
[c3] [char](7500) NOT NULL
) ON [PRIMARY]

Then I filled it with some pseudo random data, filling and the rows with 1,2,3,4, etc up to 15.

Then I create two windows to run two "almost simultaneous" transactions on two windows with the text bellow, expecting to get some deadlocks.

BEGIN TRAN
UPDATE teste SET c2=c2 WHERE c1=5
WAITFOR DELAY '00:00:05'
UPDATE teste SET c2=c2 WHERE c1=10
WAITFOR DELAY '00:00:05'
COMMIT

BEGIN TRAN
UPDATE teste SET c2=c2 WHERE c1=10
WAITFOR DELAY '00:00:05'
UPDATE teste SET c2=c2 WHERE c1=5
WAITFOR DELAY '00:00:05'
COMMIT

Now here comes the weird part: If I start with the first window (first query) and "immediately" run the second (second query) I always get a dead lock as expected, but if I start with the second window (second query) and  "immediately" run the first (first query), both transactions run fine and I don't get a dead lock.

I kept looking at the sys.dm_tran_locks but I haven't yet understood why the behaviour is different...

regards,

rdb


Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>