Académique Documents
Professionnel Documents
Culture Documents
Identify
How deadlocks work you can run the following code in the Northwind database.
To create a deadlock you can issue commands similar to the commands below.
Step Commands
begin tran
begin tran update block set sno ='7'
4
rollback
--go back to query window (2) and run these commands to undo changes
5
rollback
Solution
The only solution for handling deadlocks is to find the problem in your code and then
modify your processing to avoid deadlock situations. The first thing you need to do
is find the deadlock situations and then investigate the problem
Here is the script we used to identify the blocking 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
In our case, we killed the blocking_session_id after carefully looking at the BlockingText; it
was found to be not necessary at all. We killed the session using the following command:
KILL 66
SQL Profiler
To do this using SQL Profiler, you will need to capture the Lock Events Lock:Deadlock
and Lock:Deadlock Chain.
Description
25
Lock:Deadlock
59
Lock:Deadlock
Chain
In addition, you will also need to capture this additional column to see what objects
are part of the deadlock chain.
ColumnNumber
Column
Description
22
ObjectID
The output from our trace would show the following information:
When you have a lot of information to go through it is easier to load the data into a SQL
Server table and then query the data for the particular timeframe and SPIDs
DECLARE @lowDate AS datetime, @highDate AS datetime
SET @lowDate = '2012-07-11 20:19:17.000'
SET @highDate = '2012-07-11 20:21:18.999'
SELECT TextData, StartTime,
EndTime, SPID,
Duration,
Writes,
EventClass
FROM
TraceFile
WHERE
-- SPID IN (66) AND
(StartTime BETWEEN @lowDate AND @highDate
OR EndTime BETWEEN @lowDate AND @highDate
OR StartTime < @lowDate AND EndTime > @lowDate)
ORDER BY
StartTime
Reads,