- Transaction A holds a share lock on row 1.
- Transaction B holds a share lock on row 2.
- Transaction A requests an exclusive lock on row 2, but it cannot be granted until Transaction B releases the share lock.
- Transaction B requests an exclusive lock on row 1, but it cannot be granted until Transaction A releases the share lock.
- Each transaction must wait for the other to release the lock.
How SQL Sever Ends a Deadlock
SQL Server ends a deadlock by automatically terminating one of the transactions. The process SQL Server uses is in the following list:- Rolls back the transaction of the deadlock victim.
- In a deadlock, SQL Server gives priority to the transaction that has been processing the longest; that transaction prevails. SQL Server rolls back the transaction with the least amount of time invested.
- Notifies the deadlock victim’s application (with message number 1205).
- Cancels the deadlock victim’s current request.
- Allows the other transaction to continue.
Guidelines for Minimizing Deadlocks
- While it is not always possible to eliminate deadlocks, you can reduce the risk of a deadlock by observing the following guidelines:
- Use resources in the same sequence in all transactions. For example, in all transactions that reference more than one table, reference the tables in the same order whenever possible.
- Shorten transactions by minimizing the number of steps.
- Shorten transaction times by avoiding queries that affect many rows.
- Set the DEADLOCK_PRIORITY setting to LOW. When this setting is set and the process encounters a deadlock, it will be chosen as the deadlock victim. This is useful for online analytical processing (OLAP) database or reporting processes that you do not want to interfere with higher-priority OLTP processes.
Lock Timeout
If a transaction becomes locked while waiting for a resource and a deadlock results, SQL Server will terminate one of the participating transactions with no timeout.If no deadlock occurs, SQL Server blocks the transaction requesting the lock until the other transaction releases the lock. By default, there is no mandatory timeout period that SQL Server observes. The only way to test whether a resource that you want to lock is already locked is to attempt to access the data, which could result in getting locked indefinitely.
The LOCK_TIMEOUT setting allows an application to set a maximum time that a statement waits on a blocked resource before the blocked statement is automatically cancelled. The cancellation does not roll back or cancel the transaction. The application must trap the error to handle the timeout situation and take remedial action, such as resubmitting the transaction or rolling it back.
The following example shows how to set the lock timeout. The timeout_period is the number of milliseconds that SQL Server will wait before returning a locking error.
SET LOCK_TIMEOUT timeout_period |
The KILL command terminates a user process based on the server process ID (spid).
KILL {spid | UOW} [WITH STATUSONLY] |
No comments:
Post a Comment