我有一個複雜的工作單元,它可能會將更改提交到10-15個表作爲單個事務處理。工作單元在快照隔離下執行。異步執行SQL或從觸發器更改鎖定
某些表具有執行存儲過程以將消息記錄到隊列中的觸發器。該消息包含表名,密鑰和更改類型。這是爲了向SQL2005提供向後兼容性所必需的,我不能使用內置隊列。
問題是我正在寫入存儲過程的隊列中發生阻塞和超時。我要麼收到消息說:
Snapshot isolation transaction aborted due to update conflict. You cannot use snapshot isolation to access table 'dbo.tblObjectChanges' directly or indirectly in database
或者我得到超時寫入該表。
有沒有辦法改變從觸發器內執行消息隊列寫入的存儲過程到(或在其內部)的特定調用的事務隔離?作爲最後的手段,我可以調用刪除或更新部分存儲過程異步運行嗎?
下面是SQL存儲過程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_NotifyObjectChanges]
@ObjectType varchar(20),
@ObjectKey int,
@Level int,
@InstanceGUID varchar(50),
@ChangeType int = 2
AS
SET NOCOUNT ON
DECLARE @ObjectChangeID int
--Clean up any messages older than 10 minutes
DELETE from tblObjectChanges Where CreatedTime < DATEADD(MINUTE, -10, GetDate())
--If the object is already in the queue, change the time and instanceID
SELECT @ObjectChangeID = [ObjectChangeID] FROM tblObjectChanges WHERE [ObjectType] = @ObjectType AND [ObjectKey] = @ObjectKey AND [Level] = @Level
IF NOT @ObjectChangeID is NULL
BEGIN
UPDATE [dbo].[tblObjectChanges] SET
[CreatedTime] = GETDATE(), InstanceGUID = @InstanceGUID
WHERE
[ObjectChangeID] = @ObjectChangeID
END
ELSE
BEGIN
INSERT INTO [dbo].[tblObjectChanges] (
[CreatedTime],
[ObjectType],
[ObjectKey],
[Level],
ChangeType,
InstanceGUID
) VALUES (
GETDATE(),
@ObjectType,
@ObjectKey,
@Level,
@ChangeType,
@InstanceGUID
)
END
tblObjectChanges的定義:
CREATE TABLE [dbo].[tblObjectChanges](
[CreatedTime] [datetime] NOT NULL,
[ObjectType] [varchar](20) NOT NULL,
[ObjectKey] [int] NOT NULL,
[Rowversion] [timestamp] NOT NULL,
[Level] [int] NOT NULL,
[ObjectChangeID] [int] IDENTITY(1,1) NOT NULL,
[InstanceGUID] [varchar](50) NULL,
[ChangeType] [int] NOT NULL,
CONSTRAINT [PK_tblObjectChanges] PRIMARY KEY CLUSTERED
(
[ObjectChangeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
你不能在SQL Server 2005中使用Service Broker嗎?爲什麼不? –
你得到超時會讓我懷疑。是否有其他長時間運行的交易阻止重要數據? – usr
有問題的超時總是在調用該存儲過程時。 – Molloch