SQL server--deadlock in single reply

Source: Internet
Author: User

Many times, deadlocks are requested by two or more sessions to hold the other session of the lock while holding other session, but there are some special deadlock only by a single session lock triggered, today saw a related article, transported to share with you!

The code that caused the deadlock:

Execution Environment SQL SERVER 2012 (11.0.5058)

BEGIN TRANSACTION;GOSELECT 'CREATE TYPE EmailAddress'GOCREATETYPE EmailAddress from VARCHAR( the);GOSELECT 'CREATE Dbo.foo'GOCREATE PROCEDUREDbo.foo (@paramEmailAddress) asBEGIN     SETNOCOUNT on; DECLARE @x TABLE(e EmailAddress); INSERT  @x    SELECT  @param;END;GOSELECT 'EXEC Dbo.foo'GODECLARE @xEmailAddress;SET @x =N'whatever';EXECDbo.foo@param =N'whatever';SELECT "'GOSELECT 'ROLLBACK TRANSACTION'ROLLBACK TRANSACTION;


After executing the above code, the following deadlock view can be captured through profile:

Using sp_lock to view the lock will find:

On the metadata lock 7 (101:0:0), answer 61 has been granted the lock, but again requested the lock, resulting in the deadlock.

===================================================

This scenario rarely happens in the actual business, and no one typically puts the creation of stored procedures and user-defined types into a transaction, especially in the same transaction where the stored procedure is created and called. The way to avoid this problem is to put the creation of stored procedures and user-defined types into separate transactions to commit.

===================================================

Posture has gone up, the sister town stickers!

SQL server--deadlock in single reply

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.