How to Find a Blocking Process
Whereas the TSQL commands sp_who2, sp_lock and select * from sys.sysprocesses can provide a wealth of information when dealing with blocking processes, for less experienced DBAs the output of these queries can be quite overwhelming – especially when the we’re talking about the life and death of a process that may be deadlocked in your environment and preventing others from completing. Given the destructive nature of killing such a process, it’s very important that the correct process is identified to avoid unnecessary data loss.
The following query takes the guess work out of this and provides a very simple output that clearly identifies the offending process;
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 GO
The following is an example of the output this query generates;
From here, you can confidently eliminate the blocking process so your environment so your systems can continue operating normally using the following command;