Click here to monitor SSC

Andras

Software Architect - Red Gate Software

Self deadlocking transactions in SQL Server 2005 SP1

Published Friday, June 09, 2006 1:57 PM

Until the release of SQL Server SP1 I though I had a good understanding of how deadlocks can occur in SQL Server. Well, SP1 has changed this view quite dramatically by introducing self deadlocking transactions.

Let us look at a very simple example, in which I create an XML schema collection, and then create something that uses it as part of its schema (in this case it is a table returning function that has a strongly typed XML column). Note that such a table is not completely uncommon J, I have seen customer databases that have this.

 

BEGIN TRANSACTION a
go
CREATE
XML SCHEMA COLLECTION [dbo].[xmlSchemaCollectionA]
AS
N'
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:t="http://belokosztolszki/xx1" targetNamespace="http://belokosztolszki/xx1" elementFormDefault="qualified">
  <xsd:complexType name="complextType">
    <xsd:complexContent>
      <xsd:restriction base="xsd:anyType">
        <xsd:sequence>
          <xsd:element name="Blah" type="xsd:string" />
        </xsd:sequence>
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>'
go 

CREATE FUNCTION f ()
RETURNS
@ret TABLE (a int, b XML(CONTENT [dbo].[xmlSchemaCollectionA]))
AS
BEGIN
INSERT
INTO @ret VALUES (1, CAST(N'<blah/>' AS XML([dbo].[xmlSchemaCollectionA])))
RETURN
END
go

COMMIT

 

The result under SQL Server 2005 RTM is what one would expect, i.e. both objects are created. Under SP1 however the following greets the user:

 

Msg 1205, Level 13, State 55, Procedure f, Line 3
Transaction (Process ID 51) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Msg 3902, Level 16, State 1, Line 1
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

 

Funny, there is nothing else running. Unfortunately XML schema collections are not the only things that can cause this problem. User defined types may cause self deadlocking transactions as well. Microsoft apparently introduced this behaviour on purpose. But it does make life more difficult. Particularly for the scripts SQL Compare generates. These generated scripts migrate changes from one database to another, and need to perform operations similar to the above example. These scripts could be rather complicated, and in order to avoid inconsistent database states, we encapsulate them into a single transaction, which under SP1 may deadlock on itself, and thus may not be executed. This is rather uncomfortable, and the only workaround avaialble is to break up the transaction into several transactions, and separate the UDT or XML schema definiton from its use. Our hope is SP2 :).


Andras

by András

Comments

 

NZJon said:


Hi Andras,

One of our clients has SQL Server 2005 SP1 installed, and the SQL Packager 5.2.0.32 output from our database is, of course, having the problems you describe above. I have a few questions I'm hoping you'll be able to answer.

Firstly, do you know whether the latest version of SQL Packager 5.3, fixes the problems with Self deadlocking transactions?

Secondly, do you know whether SQL Server 2005 SP2 has fixed the issue? I haven't been able to find an MS Knowledge Base article which describes the problem or reports a hotfix for it.

Thanks for your time,

  Jon Pawley,
  Senior Software Engineer,
  Wellnomics Limited.
March 21, 2007 12:20 AM
 

András said:

Hi Jon,

unfortunately SP2 does not fix this problem, but Microsoft is aware of this bug. If executing the schema modifications transactionally is important, there is no workaround. However, if this requirement can be relaxed, you can set SQL Packager to create scripts without using transactions. (this can be set for the schema a d data separately).
  Andras
April 27, 2007 3:42 AM
 

András said:

Just another example that is concerning UDTs:

USE selfdeadlock2

BEGIN TRANSACTION selfdeadlock2

CREATE TYPE [dbo].[Idt] FROM INT NOT NULL
GO

CREATE PROCEDURE procA
AS
   BEGIN
       DECLARE @TblSurveys TABLE ( IdtSurveyType Idt ) ;

       BEGIN
SELECT 1
       END
   END
May 2, 2007 4:46 AM
 

brejk said:

Unfortunately, they do not fix the problem in SQL Server 2008. My Connect item on the problem: https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=365876.
September 8, 2008 12:11 AM
You need to sign in to comment on this blog

About András

András Belokosztolszki is a software architect at Red Gate Software Ltd. He is a frequent speaker at many UK user groups and events (VBUG, NxtGen, Developer’s Group, SQLBits). He is primarily interested in database internals and database change management. At Red Gate he has designed and led the development of many database tools that compare database schemata and enable source control for databases (SQL Compare versions 4 to 7), refactor databases (SQL Refactor) and show the history of databases by analyzing the transaction log (SQL Log Rescue). András has a PhD from Cambridge and an MSc and BSc from ELTE, Hungary. He is also a MCSD and MCPD Enterprise. See his articles on simple-talk.
Latest articles
Backups, What Are They Good For?
 We've heard the confessional story from Pixar that Toy Story 2 was almost lost due to a bad backup, but... Read more...

C# Async: What is it, and how does it work?
 The biggest new feature in C#5 is Async, and its associated Await (contextual) keyword. Anybody who is... Read more...

Handling Deadlocks in SQL Server
 In this excerpt from his book Troubleshooting SQL Server: A Guide for the Accidental DBA, Jonathan... Read more...

SQL VIEW Basics
 SQL Views are essential for the database developer. However, it is common to see them misued, or... Read more...

The PoSh DBA: Grown-Up PowerShell Functions
 Laerte goes step-by-step through the process of tidying up and making more reusable an untidy... Read more...