Database General Deadlock handling method

Source: Internet
Author: User

1. Simulate a deadlock first:

1 CREATE TABLELock1 (C1int default(0));2 CREATE TABLELock2 (C1int default(0));3 INSERT  intoLock1VALUES(1);4 INSERT  intoLock2VALUES(1);5 6 --put the following code into the two Query Analyzer windows, respectively:7 Analyze a window:8 Begin Tran9   UpdateLock1SetC1=C1+1;Ten   WaitForDelay'00:01:00'; One   SELECT *  fromLock2 A Rollback Tran; - Analysis Two window: - Begin Tran the   UpdateLock2SetC1=C1+1; -   WaitForDelay'00:01:00'; -   SELECT *  fromLock1 - Rollback Tran;

A deadlock prompt will appear after execution:

The transaction (Process ID 54) is deadlocked on the lock resource with another process and has been selected as the deadlock victim. Please rerun the transaction.

If you turn on database monitoring, you will see the deadlock graph visually, and the deadlock graph described by the database monitor can be used to derive which SQL statement is causing the deadlock.

At this point the SQL statement that caused the deadlock is found at the time of the query: SELECT * from Lock2 with (NOLOCK) (plus this) can avoid deadlocks. Note that this approach causes (dirty reads) that if the system business does not require a high level of dirty reads, this method can be used to resolve deadlocks.

Database General Deadlock handling method

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.