INF: Deadlock Scenarios and Avoidance in SQL
ID: Q47162
|
The information in this article applies to:
-
Microsoft SQL Server version 4.2x
SUMMARY
This article describes common deadlock scenarios and avoidance
techniques.
In the classic deadlock, a transaction locks record 1 and later locks
record 2. If a second transaction attempts to lock the same two
records, but in the opposite order, and happens to run just after the
first transaction acquired its first lock, a deadlock occurs.
In a single-server environment, SQL Server detects this kind of
deadlock and abnormally terminates one of the transactions. If a
single client application is performing updates on multiple SQL
servers, permanent deadlocks are possible because the participating
SQL Servers do not communicate with each other regarding lock status
and requests. This is true even if 2 phase commit service is being
used.
MORE INFORMATION
Consider the following distributed update example:
- An application performs updates on its local server and also on a
remote server.
- When another copy of that application runs on the other server, a
deadlock occurs because "local" and "remote" are relative to each
application.
In absolute terms, the two applications are requesting their locks in
opposite order.
To correct this problem, request locks in the same absolute order.
It is also possible for two applications to deadlock even if they request
locks in the same order, as in the following example:
- An application reads a record with holdlock (to prevent others from
updating what it has read) and later updates the record.
- Two copies of this application can deadlock because both can get
the readlock, but neither can update (each is waiting for the other
to release the readlock).
SQL Server detects this kind of deadlock on a single server; however,
it cannot do so if the lock dependencies span multiple servers.
To correct this problem, use SQL Server BROWSE mode or timestamps
rather than read with holdlock. For more information, refer to the
"Maximizing Database Consistency and Concurrency" manual that ships
with SQL Server version 4.2.
Additional query words:
4.20 wait
Keywords : kbother SSrvLock
Version : 4.20
Platform :
Issue type :
Last Reviewed: March 6, 1999