日期:2014-05-18  浏览次数:20586 次

数据库进程死锁
应用场景如下:使用BizTalk从webservice接口获取数据,在存储过程GetExchangeConfig中设置每次获取的数据数量以及开始时间,获取数据后,Update获取数据的开始时间,使用存储过程UpdateExchangeConfig,在存储过程GetExchangeConfig中对开始时间进行Select操作,在存储过程UpdateExchangeConfig中对开始时间进行Update操作,产生数据库死锁,导致数据获取中断。
SQL code

USE [NotaryBusiness]
GO
/****** 对象:  StoredProcedure [dbo].[GetExchangeConfig]    脚本日期: 01/26/2011 17:26:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetExchangeConfig]
    /*
    (
    @parameter1 int = 5,
    @parameter2 datatype OUTPUT
    )
    */
AS
    /* SET NOCOUNT ON */
    select [Count],TimeBegin
    from ExchangeConfig
    for xml auto
    RETURN


SQL code

USE [NotaryBusiness]
GO
/****** 对象:  StoredProcedure [dbo].[UpdateExchangeConfig]    脚本日期: 01/26/2011 17:27:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UpdateExchangeConfig]
    @TimeBegin as datetime
AS
    update ExchangeConfig set TimeBegin=@TimeBegin
    RETURN



------解决方案--------------------
第一个存储过程修改:

SQL code
USE [NotaryBusiness]
GO
/****** 对象:  StoredProcedure [dbo].[GetExchangeConfig]    脚本日期: 01/26/2011 17:26:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetExchangeConfig]
    /*
    (
    @parameter1 int = 5,
    @parameter2 datatype OUTPUT
    )
    */
AS
    /* SET NOCOUNT ON */
    select [Count],TimeBegin
    from ExchangeConfig WITH (NOLOCK)
    for xml auto
    RETURN