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 the architect of SQL Compare versions 4, 5, 6 and 7, SQL Log Rescue and SQL Refactor. He is focused on database internals, database synchronization and database schema evolution.

















<June 2006>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
On the Trail of the Expanding Databases
 It is sometimes difficult for other IT people to understand the constraints that DBAs have to work... Read more...

SQL Server 2008: The New Data Types
 Brad continues his helicopter-level view of the most interesting new features of SQL Server 2008 with a... Read more...

Reporting on Mobile Device Activity Using Exchange 2007 ActiveSync Logs
 In this new column giving practical advice on all things Sys Admin related, Ben Lye takes on the often... Read more...

The Bejeweled Puzzle in SQL
 Alex Kozak provides another SQL puzzle to hone your SQL Skills with.  Read more...

Using Powershell to Generate Table-Creation Scripts
 For all of us who learn best by trying out examples, Bob Sheldon produces a PowerShell script file for... Read more...