In our production server, the deadlock happening frequently. Is anyone can help on how to view those transactions from which the deadlock occur?
Asked
Active
Viewed 9,469 times
2
-
1See: http://stackoverflow.com/questions/720508/help-with-deadlock-in-sql-server-2008 – S.K Jul 23 '15 at 06:52
2 Answers
4
Use this query
SELECT db.name DBName,
tl.request_session_id,
wt.blocking_session_id,
Object_name(p.OBJECT_ID) BlockedObjectName,
tl.resource_type,
h1.TEXT AS RequestingText,
h2.TEXT AS BlockingTest,
tl.request_mode
FROM sys.dm_tran_locks AS tl
INNER JOIN sys.databases db
ON db.database_id = tl.resource_database_id
INNER JOIN sys.dm_os_waiting_tasks AS wt
ON tl.lock_owner_address = wt.resource_address
INNER JOIN sys.partitions AS p
ON p.hobt_id = tl.resource_associated_entity_id
INNER JOIN sys.dm_exec_connections ec1
ON ec1.session_id = tl.request_session_id
INNER JOIN sys.dm_exec_connections ec2
ON ec2.session_id = wt.blocking_session_id
CROSS APPLY sys.Dm_exec_sql_text(ec1.most_recent_sql_handle) AS h1
CROSS APPLY sys.Dm_exec_sql_text(ec2.most_recent_sql_handle) AS h2

Pரதீப்
- 91,748
- 19
- 131
- 172
-
-
I'm afraid I have to disagree with this answer (and thus the downvote - sorry!). The query that you have deals with blocking, whereas the OP asked about deadlocks. You can't query about deadlocks out of a DMV because, by definition, one of the processes gets killed and is thus no longer available in the system. – Ben Thul Jul 23 '15 at 16:53
1
I get using these three methods:
Get active deadlocks:
SELECT
SESSION_ID
,BLOCKING_SESSION_ID
FROM SYS.DM_EXEC_REQUESTS
WHERE BLOCKING_SESSION_ID != 0
Get text of the query (And check if it is really important):
exec sp_whoisactive |session_id|
Kill the deadlock (Use wisely):
kill |session_id|
Also, the previous answer didn't work fine (I don't know why), I've executed this:
Query 1:
begin tran
create table ##a (i int)
insert ##a values (1)
Query 2:
delete ##a
Query 3: The answer

Greenonline
- 1,330
- 8
- 23
- 31

Erick de Vathaire
- 166
- 2
- 5