Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15694

"Deadlock" example follows. Need help with Analysis.

$
0
0

To betterunderstand deadlocks I created a simple example consisting of three Sql 2005 queries, each run in its own query window  (different SPIDs). My analysis and questions about the ERRORLOG follow in red.

TIA,

edm2

 

 

-- Setup for the two queries that follow: (ran as SPID 55)

dbcc traceon(1222,-1)

Create table DeadlockTest (id int, Name Varchar(5))

Insert into DeadlockTest values (1, 'E')
Insert into DeadlockTest values (2, 'F')
Insert into DeadlockTest values (3, 'G')

GO

  


-- Query 1, SPID 53
Begin Tran
Update DeadlockTest
SET Name = 'A'
Where id = 3
WAITFOR DELAY '00:00:20'
Update DeadlockTest
SET Name = 'B'
Where id = 1

 

-- Query 2, SPID 54  (deadlock victim)
Begin Tran
Update DeadlockTest
SET Name = 'B'
Where id = 1
WAITFOR DELAY '00:00:20'
Update DeadlockTest
SET Name = 'A'
Where id = 3

Msg 1205, Level 13, State 45, Line 3
Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

 

ERRORLOG:

2013-03-03 09:27:02.21 spid55     DBCC TRACEON 1222, server process ID (SPID) 55. This is an informational message only; no user action is required.
2013-03-03 09:28:26.09 spid16s     deadlock-list
2013-03-03 09:28:26.09 spid16s      deadlock victim=process803c9708
2013-03-03 09:28:26.09 spid16s       process-list
2013-03-03 09:28:26.09 spid16s        process id=process803c9708 taskpriority=0 logused=232waitresource=RID: 5:1:153:2 waittime=20295 ownerId=14790 transactionname=user_transaction lasttranstarted=2013-03-03T09:28:05.630 XDES=0x815f9740lockMode=U schedulerid=1 kpid=2336 status=suspendedspid=54 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2013-03-03T09:28:05.630 lastbatchcompleted=2013-03-03T09:25:17.593 lastattention=2013-03-03T08:56:34.733 clientapp=Microsoft SQL Server Management Studio - Query hostname=SQL2008 hostpid=2788 loginname=TestSql2005Server isolationlevel=read committed (2) xactid=14790 currentdb=5 lockTimeout=4294967295 clientoption1=671090784 clientoption2=390200
2013-03-03 09:28:26.09 spid16s         executionStack
2013-03-03 09:28:26.09 spid16s          frame procname=adhoc line=3 stmtstart=58 sqlhandle=0x020000008b36222686e003687e753e8acf2951b9994f41f8
2013-03-03 09:28:26.09 spid16s     UPDATE [DeadlockTest] set [Name] = @1  WHERE [id]=@2   

**** How can I determine the values of @1 and @2 ?

 
2013-03-03 09:28:26.10 spid16s          frame procname=adhoc line=3 stmtstart=48 stmtend=154 sqlhandle=0x02000000799ee11f410009bfcf446b4ccf2337f1f9ab4912
2013-03-03 09:28:26.10 spid16s     Update DeadlockTest
2013-03-03 09:28:26.10 spid16s     SET Name = 'B'
2013-03-03 09:28:26.10 spid16s     Where id = 1    
2013-03-03 09:28:26.10 spid16s         inputbuf

**** Why is this Update singled out? (This very Query is included below in the full query)


2013-03-03 09:28:26.10 spid16s     -- Query 2
2013-03-03 09:28:26.10 spid16s     Begin Tran
2013-03-03 09:28:26.10 spid16s     Update DeadlockTest
2013-03-03 09:28:26.10 spid16s     SET Name = 'B'
2013-03-03 09:28:26.10 spid16s     Where id = 1
2013-03-03 09:28:26.10 spid16s     WAITFOR DELAY '00:00:20'
2013-03-03 09:28:26.10 spid16s     Update DeadlockTest
2013-03-03 09:28:26.10 spid16s     SET Name = 'A'
2013-03-03 09:28:26.10 spid16s     Where id = 3


2013-03-03 09:28:26.10 spid16s        process id=process803c9dc8 taskpriority=0 logused=232 waitresource=RID: 5:1:153:0 waittime=1584 ownerId=14780 transactionname=user_transaction lasttranstarted=2013-03-03T09:28:04.330 XDES=0x815f8e90lockMode=U schedulerid=1 kpid=2320 status=suspendedspid=53 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2013-03-03T09:28:04.330 lastbatchcompleted=2013-03-03T09:25:10.943 lastattention=2013-03-03T08:56:37.397 clientapp=Microsoft SQL Server Management Studio - Query hostname=SQL2008 hostpid=2788 loginname=TestSql2005Server isolationlevel=read committed (2) xactid=14780 currentdb=5 lockTimeout=4294967295 clientoption1=671090784 clientoption2=390200
2013-03-03 09:28:26.10 spid16s         executionStack
2013-03-03 09:28:26.10 spid16s          frame procname=adhoc line=7 stmtstart=58 sqlhandle=0x020000008b36222686e003687e753e8acf2951b9994f41f8
2013-03-03 09:28:26.10 spid16s     UPDATE [DeadlockTest] set [Name] = @1  WHERE [id]=@2    
2013-03-03 09:28:26.10 spid16s          frame procname=adhoc line=7 stmtstart=210 sqlhandle=0x020000009af20e2fcb754ebe1a6d627075a3bbbc4604ac0e
2013-03-03 09:28:26.10 spid16s     Update DeadlockTest
2013-03-03 09:28:26.10 spid16s     SET Name = 'B'
2013-03-03 09:28:26.10 spid16s     Where id = 1    
2013-03-03 09:28:26.10 spid16s         inputbuf
2013-03-03 09:28:26.10 spid16s     -- Query 1
2013-03-03 09:28:26.10 spid16s     Begin Tran
2013-03-03 09:28:26.10 spid16s     Update DeadlockTest
2013-03-03 09:28:26.10 spid16s     SET Name = 'A'
2013-03-03 09:28:26.10 spid16s     Where id = 3
2013-03-03 09:28:26.10 spid16s     WAITFOR DELAY '00:00:20'
2013-03-03 09:28:26.10 spid16s     Update DeadlockTest
2013-03-03 09:28:26.10 spid16s     SET Name = 'B'
2013-03-03 09:28:26.10 spid16s     Where id = 1


2013-03-03 09:28:26.10 spid16s       resource-list
2013-03-03 09:28:26.10 spid16s        ridlock fileid=1 pageid=153 dbid=5

objectname=TestDB.dbo.DeadlockTest id=lock80096c00 mode=X associatedObjectId=72057594039107584
2013-03-03 09:28:26.10 spid16s         owner-list

**** process803c9dc8 is SPID 53 and process id=process803c9708 is SPID 54 (victim)

It appears that SPID 53 has an exclusive lock on a table row (indicated by ridlock fileid=1 pageid=153 dbid=5) and SPID 54 wants to apply an Update lock to the same resource.


2013-03-03 09:28:26.10 spid16s          owner id=process803c9dc8 mode=X
2013-03-03 09:28:26.10 spid16s         waiter-list
2013-03-03 09:28:26.10 spid16s          waiter id=process803c9708 mode=U requestType=wait
2013-03-03 09:28:26.10 spid16s        ridlock fileid=1 pageid=153 dbid=5

It also appears there is some other resource (associatedObjectId=72057594039107584) that SPID 54 owns that SPID 53 wants to place an Update lock on. Unless the latter resource (“associatedObjectId”) is actually the same as ridlock mentioned above, why is there a deadlock at all?

objectname=TestDB.dbo.DeadlockTest id=lock80096480 mode=X associatedObjectId=72057594039107584
2013-03-03 09:28:26.10 spid16s         owner-list
2013-03-03 09:28:26.10 spid16s          owner id=process803c9708 mode=X
2013-03-03 09:28:26.10 spid16s         waiter-list
2013-03-03 09:28:26.10 spid16s          waiter id=process803c9dc8 mode=U requestType=wait

 

 

 

 

 


Viewing all articles
Browse latest Browse all 15694

Trending Articles



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