
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) Given the following basic table schema: CREATE TABLE. Basic Deadlock Simulationīy way of a very simple scenario, you can make use of SQL Management Studio (and breakpoints) to roughly simulate a deadlock scenario. Note that the scope of a non-startup trace flag can be global or session-level. So to enable or disable deadlock trace flags globally, you’d use the following T-SQL: DBCC TRACEON (1204, -1)ĭue to the overhead, it’s best to enable the flag at runtime rather than on start up. You cannot enable a session-level trace flag by using a startup option. The -T startup option enables a trace flag globally. Using the -T startup option to specify that the trace flag be set on during startup.Using the DBCC TRACEON and DBCC TRACEOFF commands.įor example, DBCC TRACEON 2528: To enable the trace flag globally, use DBCC TRACEON with the -1 argument: DBCC TRACEON (2528, -1). To turn off a global trace flag, use DBCC TRACEOFF with the -1 argument.Trace flags are set on or off by using either of the following methods: They are (usually) set temporarily, and they cause deadlocking information to be dumped to the SQL management logs.

However, the generally accepted way to determine and diagnose deadlocks is through the use of SQL Server trace flags. This will show you line and verse (the actual statement causing the resource block) – see the attached screenshot for an example. ON ssion_id = BlockedReqs.blocking_session_idĬROSS APPLY sys.dm_exec_sql_text(Conns.most_recent_sql_handle) AS Blocker INNER JOIN sys.dm_exec_requests AS BlockedReqs To get to the heart of the deadlock, this is what you want (courtesy of this SO question/answer): Which is not particularly useful (but good to know, so you can see the blocked count). The following T-SQL will show you the “victim” processes, much like activity monitor does: select * from sys.sysprocesses where blocked > 0 This can be a serious condition, not just for SQL Server as processes become suspended, but for the applications which rely on SQL Server as well.Ī fast way to respond is to execute a bit of T-SQL on SQL Server, making use of System Views. The Database Engine chooses one of the sessions as a deadlock victim and the current transaction is terminated with an error to break the deadlock.īasically, it’s a resource contention issue which blocks one process or transaction from performing actions on resources within SQL Server. The SQL Server Database Engine automatically detects deadlock cycles within SQL Server. Task T2 has a lock on resource R2 (indicated by the arrow from R2 to T2) and has requested a lock on resource R1 (indicated by the arrow from T2 to R1).īecause neither task can continue until a resource is available and neither resource can be released until a task continues, a deadlock state exists. Task T1 has a lock on resource R1 (indicated by the arrow from R1 to T1) and has requested a lock on resource R2 (indicated by the arrow from T1 to R2). The following graph presents a high level view of a deadlock state where:



Just let me know.īefore we get into diagnosis and investigation, it’s a good time to pose the question: “what is a deadlock?”:Ī deadlock occurs when two or more tasks permanently block each other by each task having a lock on a resource which the other tasks are trying to lock. If there’s a lot of interest in exploring SQL Server and deadlocks further, I’m happy to write an extended article going into far more detail. Recently I was asked about diagnosing deadlocks in SQL Server – I’ve done a lot of work in this area way back in 2008, so I figure it’s time for a refresher.
