Solutions to SQL Server table deadlock

Source: Internet
Author: User

I wrote a stored procedure the other day and used transactions in the stored procedure.CodeComment out to debug and find the error. Suddenly, a table is locked. It turns out that the code for creating the transaction has forgotten to comment out. Solution to table lock in this article.

In fact, not only will the table be locked as described above, but there are also many scenarios that will cause a deadlock in the Table. Unlocking is actually very simple. The following example is used to explain:
 

1. First create a test table:
Copy the Code as follows:
Create Table Test
(
TID int identity (1, 1)
)

2. Execute the following SQL statement to lock the table:
Copy the Code as follows:
Select * from test with (tablockx)

3. Use the following statement to check which tables in the current database have deadlocks:
Copy the Code as follows:
Select request_session_id spid, object_name (resource_associated_entity_id) tablename
From SYS. dm_tran_locks
Where resource_type = 'object'

4. The execution result of the preceding statement is as follows:

 

Spid: the ID of the locked process.
Tablename: name of the table in which a deadlock occurs.

5. You only need to use the kill keyword to kill the locked process ID to unlock the table:

Kill 52

Reproduced to: http://www.jb51.net/article/28163.htm

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.