Chapter 42. Tracing the deadlock

 

Gail Shaw

Msg 1205, Level 13, State 45, Line 5
Transaction (Process ID 53) was deadlocked on lock resources with another
process and has been chosen as the deadlock victim. Rerun the transaction.

A deadlock has to be one of the most frustrating error messages that SQL Server can produce. Little information is given as to what went wrong, and the only advice the error message gives is “Rerun the transaction.” It appears that there’s no way to find the root cause and to prevent the mysterious error from occurring.

In truth, there are ways to find information on exactly what happened to cause a deadlock and, with that information, in most cases it’s possible to fix the root cause and prevent the error completely.

What’s a deadlock?

At its simplest, a deadlock refers to a locking situation that, if no outside action is taken, will never resolve itself. It occurs when two or more processes have a lock on a resource and then try to acquire a lock on the resource held by the other process, or when two or more processes hold shared locks on a resource and attempt to convert them to exclusive locks. Figure 1 illustrates an example of a deadlock scenario.

Figure 1. The first and second stages of a deadlock

Causes of deadlocks

Deadlock graph

The big picture

Summary

About the author