Causes and solutions of deadlock in SQL Server

Source: Internet
Author: User
Causes and resolutions of deadlock in SQL Server 2006-07-18 05:12:10

Category: SQL Server

In fact, the deepest cause of all deadlocks is one: resource competition

Performance One:

A User A accesses table A (locks up table A), then accesses table B, another user B accesses table B (locked table B), and then attempts to access table A, when user a because User B has locked table B, it must wait for User B to release table B before it can continue, well, then he will have to be honest and wait here. The same user B waits for user A to release form A to continue this is a deadlock.

Workaround:

This deadlock is due to a bug in your program, except that you have to adjust the logic of your program.

Carefully analyze the logic of your program:

1: Try to avoid locking up to two resources at a time

2: When two resources must be locked at the same time, ensure that the resources are locked in the same order at any one time.

Performance Two:

User A reads a record and then modifies the record. This is the User B modifies the record, where the nature of the lock in User A's transaction is raised by a shared lock attempt to an exclusive lock (for update), while the exclusive lock in User B must wait for a to release the shared lock because a has a shared lock, and a because the exclusive lock of B cannot rise the exclusive lock can not release the shared lock, So there was a deadlock.

This kind of deadlock is relatively covert, but in fact, in a slightly larger project often occurs.

Workaround:

Let user A's transaction (that is, the operation of the first read and write type), when the Select is the update lock

The syntax is as follows:

SELECT * FROM table1 with (Updlock) where .... [@[email protected]]

Causes and solutions of deadlock in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.