SQLConfiguration SQLConfiguration


Transactions Locks «Prev  Next»
Lesson 7 Deadlocks
Objective Handle deadlocks.

Deadlock Transaction Blocking

What is a deadlock?

A deadlock is a situation where one transaction is blocking another from continuing, but that transaction is blocking the first from continuing. It is like a stalemate in chess.
A deadlock is a special situation that occurs when two or more processes are competing for the same set of resources; each prevents the other from obtaining the source it needs to complete its work. Deadlocks are not a relational database specific problem; they can occur in any system where there is potential for resource contention, such as operating systems. However, because this is course deals specifically with SQL Server, and we will focus on the deadlock nuances inside the database engine.

Deadlock Example

Following is a simple example of a common deadlock scenario:
  1. Transaction 1 has a lock on data A and needs to lock data B to complete its transaction.
  2. Transaction 2 has a lock on data B and needs to lock data A to complete its transaction.

Each transaction is stuck waiting for the other to release its lock, and neither can complete until the other does, and each process will not release the resource it already has for the other process to use.
Deadlocks do not always only include two transactions. It is completely possible that Process A can be waiting on a resource held by Process B, which is, in turn, waiting on a resource held by Process C. If C is waiting on a resource that Process A or Process B has locked, a deadlock is created.

What is Blocking?

Blocking occurs when two connections need access to same element of data concurrently and one connection is blocked because at a specific time, only one connection can have access. Figure 2.7.1 depicts cars moving through a roundabout, where some cars must pause in order to allow the other cars to traverse the lane which is open.

Figure 2.7.1: Roundabout intersection depicts Blocking

Graphic Analogy for Deadlock

Deadlock (as depicted in Figure 2.7.2) occurs when one connection is blocked while waiting for a second task to complete its work. The second connection is waiting for the first connection to release the lock and cannot proceed until this happens. This is similar to when you need to cross a traffic stop, but at the same time someone else from the opposite side also wants to cross the intersection. One automobile must pause in order to allow another vehcile to pass through its lane.

Figure 2.7.2: Automobiles at an intersection depicting a deadlock

Escaping a deadlock

Once a deadlock occurs, the only way out is to continue to rollback prior transactions until the lock is cleared. MS SQL Server 2016 automatically does this for you when a deadlock occurs. SQL Server waits until a specified timeout period to keep trying to see if the lock is freed up. This time period is adjustable and can be viewed through the @@lock_timeout global variable, which will return the timeout period in milliseconds.

Setting the timeout

You set the timeout by using the SET LOCK_TIMEOUT Transact-SQL statement, followed by the number of milliseconds that the system should wait before rolling back blocked transactions. For example, if you wanted to set the timeout to 5 seconds (or 5000 milliseconds), use this Transact-SQL statement:

SET LOCK_TIMEOUT 5000

You can then test to see what the value of the lock timeout period is by issuing this Transact-SQL statement:
SELECT @@LOCK_TIMEOUT

If the value of @@LOCK_TIMEOUT is –1, this indicates that the value has not yet been set for the session. The next lesson will show you how to avoid deadlocks.

Understanding Lock Modes

Beyond considering just what resource level you are locking, you also should consider what lock mode your query is going to acquire. Just as there are a variety of resources to lock, there are also a variety of lock modes. Some modes are exclusive of each other (which means they do not work together). Some modes do nothing more than essentially modify other modes. Whether modes can work together is based on whether they are compatible . Just as you did with lockable resources, you will next take a look at lock modes one by one.

Understanding Shared Locks

This is the most basic type of lock there is. A shared lock is used when you need only to read the data. That is, you will not be changing anything. A shared lock wants to be your friend, as it is compatible with other shared locks. That does not mean that it still will not cause you grief , although a shared lock doesn’t mind any other kind of lock, there are other locks that do not like shared locks. Shared locks tell other locks that you are out there. They do not serve much of a purpose, yet they cannot really be ignored. However, one thing that shared locks do is prevent users from performing dirty reads.[2]

[1]Deadlock: A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does.

[2]A dirty read (also known as uncommitted dependency) occurs when a transaction is allowed to read data from a row that has been modified by another running transaction and not yet committed.