Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 April SQL Server: 8 different ways to Detect a DeadLock in a Database

SQL Server: 8 different ways to Detect a DeadLock in a Database

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In the previous post, I shared T-SQL script to generate a DeadLock situation in SQL Server Database.
In this post, I am sharing different scripts to detect a Deadlock in SQL Server.

1. Using SP_LOCK, you can find the WAIT status for blocking sessions:

1
SP_LOCK

SQL Server SP_Lock

2. Using sys.sysprocesses:

1
2
SELECT * FROM sys.sysprocesses
WHERE blocked > 0

3. Using common DMV:

1
2
3
4
5
6
7
8
9
SELECT
der.blocking_session_id AS BlockingSessionID
,dest.text AS BlockingStatement
FROM sys.dm_exec_connections AS sdec
INNER JOIN sys.dm_exec_requests AS der
ON sdec.session_id = der.blocking_session_id
INNER JOIN sys.dm_os_waiting_tasks AS dowt
ON der.session_id = dowt.session_id
CROSS APPLY sys.dm_exec_sql_text(sdec.most_recent_sql_handle) AS dest

4. Using sys.dm_tran_locks:

1
2
3
4
5
6
SELECT
request_session_id AS SPID
,DB_NAME(resource_database_id) AS DatabaseName
,resource_type AS LockedResource
,request_mode AS LockType
FROM sys.dm_tran_locks

5. Enable required trace flags to log DeadLock related information in Tracefile:

1
2
DBCC TRACEON (1204, -1)
DBCC TRACEON (1222, -1)

6. Count total number of DeadLock:

1
2
3
4
SELECT cntr_value AS TotalNumberOfDeadLocks
FROM sys.dm_os_performance_counters
WHERE counter_name = 'Number of Deadlocks/sec'
AND instance_name = '_Total'

7. Using Extended Events and below script to create Extended Event for monitoring the DeadLock:

1
2
3
4
5
6
7
8
CREATE EVENT SESSION [Deadlock_Monitor] ON SERVER
ADD EVENT sqlos.scheduler_monitor_deadlock_ring_buffer_recorded,
ADD EVENT sqlserver.lock_deadlock,
ADD EVENT sqlserver.lock_deadlock_chain,
ADD EVENT sqlserver.xml_deadlock_report
ADD TARGET package0.event_file(SET filename=N'C:\Temp\Deadlock_Monitor.xel')
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=ON)
GO

SQL Server Extended Event For Deadlock

You can also use SSMS to create Extended Events for DeadLock, and you can monitor the live status of the server.

SQL Server Extended Event For Deadlock In SSMS

8. Using SQL Server Profiler:
SQL Server Profiler has three different events to capture a deadlock.

1
2
3
1.Deadlock graph
2.Lock: Deadlock
3.Lock: Deadlock Chain
Apr 9, 2016Anvesh Patel
SQL Server: T-SQL script to generate a DeadLock in a DatabaseSQL Server: What happens after the Deadlock arise?
Comments: 1
  1. Priya
    December 29, 2016 at 10:50 am

    thank you sir, one of the best article and blog……

Anvesh Patel
Anvesh Patel

Database Engineer

April 9, 2016 SQL ServerAnvesh Patel, database, database research and development, dbrnd, DeadLock, dm_os_performance_counters, DMV, extended event, sp_lock, SQL Profiler, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....