如何在单个进程中模拟SQL Server中的死锁?

Pau*_*ams 27 sql sql-server unit-testing deadlock raiserror

我们的客户端代码检测到死锁,等待一段时间,然后重试请求最多5次.重试逻辑基于错误号1205检测死锁.

我的目标是在各种存储过程中测试死锁重试逻辑和死锁处理.我可以使用两个不同的连接创建死锁.但是,我想在单个存储过程本身内部模拟死锁.

死锁引发以下错误消息:

消息1205,第13级,状态51,第1行
事务(进程ID 66)在锁资源上与另一个进程发生死锁,并被选为死锁受害者.重新运行该交易.

我看到此错误消息在sys.messages:

select * from sys.messages where message_id = 1205 and language_id = 1033

message_id language_id severity  is_event_logged   text
1205       1033        13        0                 Transaction (Process ID %d) was deadlocked on %.*ls resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Run Code Online (Sandbox Code Playgroud)

我不能使用RAISERROR以下方法引发此错误:

raiserror(1205, 13, 51)
Run Code Online (Sandbox Code Playgroud)

消息2732,级别16,状态1,行1
错误号1205无效.号码必须是13000到2147483647,不能是50000.

我们的死锁重试逻辑检查错误号是否为1205.死锁需要具有与正常死锁相同的消息ID,级别和状态.

有没有办法模拟死锁(使用RAISERROR或任何其他方法)并只用一个进程获取相同的消息号?

我们的数据库使用SQL 2005兼容性,但我们的服务器在2005年到2008年之间有所不同.

Pau*_*ams 49

正如许多人所指出的那样,答案是否定的,单个进程无法可靠地陷入僵局.我想出了以下解决方案来模拟开发或测试系统上的死锁.

在SQL Server Management Studio窗口中运行下面的脚本.(仅在2008 R2上测试过.)您可以根据需要让它保持运行.

在您想要模拟死锁的位置,插入一个调用sp_simulatedeadlock.运行您的进程,应该发生死锁.

完成测试后,停止SSMS查询并在底部运行清理代码.

/*
This script helps simulate deadlocks.  Run the entire script in a SQL query window.  It will continue running until stopped.
In the target script, insert a call to sp_simulatedeadlock where you want the deadlock to occur.
This stored procedure, also created below, causes the deadlock.
When you are done, stop the execution of this window and run the code in the cleanup section at the bottom.
*/
set nocount on

if object_id('DeadlockTest') is not null
    drop table DeadlockTest

create table DeadlockTest
(
    Deadlock_Key int primary key clustered,
    Deadlock_Count int
)
go

if exists (select * from sysobjects where id = object_id(N'sp_simulatedeadlock')
           AND objectproperty(id, N'IsProcedure') = 1)
drop procedure sp_simulatedeadlock
GO

create procedure sp_simulatedeadlock
(
    @MaxDeadlocks int = -1 -- specify the number of deadlocks you want; -1 = constant deadlocking
)
as begin

    set nocount on

    if object_id('DeadlockTest') is null
        return

    -- Volunteer to be a deadlock victim.
    set deadlock_priority low

    declare @DeadlockCount int

    select @DeadlockCount = Deadlock_Count -- this starts at 0
    from DeadlockTest
    where Deadlock_Key = 2

    -- Trace the start of each deadlock event.
    -- To listen to the trace event, setup a SQL Server Profiler trace with event class "UserConfigurable:0".
    -- Note that the user running this proc must have ALTER TRACE permission.
    -- Also note that there are only 128 characters allowed in the trace text.
    declare @trace nvarchar(128)

    if @MaxDeadlocks > 0 AND @DeadlockCount > @MaxDeadlocks
    begin

        set @trace = N'Deadlock Test @MaxDeadlocks: ' + cast(@MaxDeadlocks as nvarchar) + N' @DeadlockCount: ' + cast(@DeadlockCount as nvarchar) + N' Resetting deadlock count.  Will not cause deadlock.'
        exec sp_trace_generateevent
            @eventid = 82,  -- 82 = UserConfigurable:0 through 91 = UserConfigurable:9
            @userinfo = @trace

        -- Reset the number of deadlocks.
        -- Hopefully if there is an outer transaction, it will complete and persist this change.
        update DeadlockTest
        set Deadlock_Count = 0
        where Deadlock_Key = 2
        return
    end

    set @trace = N'Deadlock Test @MaxDeadlocks: ' + cast(@MaxDeadlocks as nvarchar) + N' @DeadlockCount: ' + cast(@DeadlockCount as nvarchar) + N' Simulating deadlock.'
    exec sp_trace_generateevent
        @eventid = 82,  -- 82 = UserConfigurable:0 through 91 = UserConfigurable:9
        @userinfo = @trace

    declare @StartedTransaction bit
    set @StartedTransaction = 0
    if @@trancount = 0
    begin
        set @StartedTransaction = 1
        begin transaction
    end

    -- lock 2nd record
    update DeadlockTest
    set Deadlock_Count = Deadlock_Count
    from DeadlockTest
    where Deadlock_Key = 2

    -- lock 1st record to cause deadlock
    update DeadlockTest
    set Deadlock_Count = Deadlock_Count
    from DeadlockTest
    where Deadlock_Key = 1

    if @StartedTransaction = 1
        rollback    
end
go

insert into DeadlockTest(Deadlock_Key, Deadlock_Count)
select 1, 0
union select 2, 0

-- Force other processes to be the deadlock victim.
set deadlock_priority high

begin transaction

while 1 = 1
begin

    begin try

        begin transaction

        -- lock 1st record
        update DeadlockTest
        set Deadlock_Count = Deadlock_Count
        from DeadlockTest
        where Deadlock_Key = 1

        waitfor delay '00:00:10'

        -- lock 2nd record (which will be locked when the target proc calls sp_simulatedeadlock)
        update DeadlockTest
        set Deadlock_Count = Deadlock_Count
        from DeadlockTest
        where Deadlock_Key = 2

        rollback

    end try
    begin catch
        print 'Error ' + convert(varchar(20), ERROR_NUMBER()) + ': ' + ERROR_MESSAGE()
        goto cleanup
    end catch

end

cleanup:

if @@trancount > 0
    rollback

drop procedure sp_simulatedeadlock
drop table DeadlockTest
Run Code Online (Sandbox Code Playgroud)


Mic*_*art 13

您可以利用Microsoft似乎并不急于通过运行来修复的错误

use tempdb

begin tran
go

CREATE TYPE dbo.IntIntSet AS TABLE(
    Value0 Int NOT NULL,
    Value1 Int NOT NULL
)
go

declare @myPK dbo.IntIntSet;
go

rollback
Run Code Online (Sandbox Code Playgroud)

此SQL将导致自身死锁.在Aaron Bertand的博客http://sqlperformance.com/2013/11/t-sql-queries/single-tx-deadlock上有更多细节


小智 5

(显然我没有足够的声誉来添加评论.所以发帖作为答案.)

死锁至少需要两个进程.唯一的例外是查询内并行死锁,这种死锁是无法再现的.

但是,您可以在运行完全相同的查询(或sp)的两个进程上模拟死锁.这里有一些想法