中止快照隔离事务-更新“父”并插入“子”

时间:2018-07-04 13:54:34

标签: sql-server tsql

给出以下数据模型:

enter image description here

我们使用“快照隔离”运行两个单独的事务。

事务1:插入“儿童”记录。

SET TRANSACTION ISOLATION LEVEL SNAPSHOT

BEGIN TRAN
WAITFOR DELAY '00:00:05'

INSERT INTO [Snapshotsch].[dbo].[Child] ([Id], [Name], [ParentId])
VALUES (NEWID(), 'AAAA', 'A130AC13-F2E4-4ABD-8D5B-8DD33E472C58')

WAITFOR DELAY '00:00:05'

INSERT INTO [Snapshotsch].[dbo].[Child] ([Id], [Name], [ParentId])
VALUES (NEWID(), 'BBBB', 'A130AC13-F2E4-4ABD-8D5B-8DD33E472C58')

WAITFOR DELAY '00:00:05'

COMMIT TRAN

交易2:“父母”记录的更新。

SET TRANSACTION ISOLATION LEVEL SNAPSHOT

BEGIN TRAN
WAITFOR DELAY '00:00:05'

UPDATE [Snapshotsch].[dbo].[Parent]
SET [Name] = 'AAAAA'
WHERE [Id] = 'A130AC13-F2E4-4ABD-8D5B-8DD33E472C58'

WAITFOR DELAY '00:00:05'

COMMIT TRAN

当我同时运行这些事务时,将失败并显示错误:

Msg 3960, Level 16, State 2, Line 6
Snapshot isolation transaction aborted due to update conflict. 
You cannot use snapshot isolation to access table 'dbo.Parent' 
directly or indirectly in database 'Snapshotsch' to update, 
delete, or insert the row that has been modified or deleted 
by another transaction. Retry the transaction or change the 
isolation level for the update/delete statement.

我们可以做些什么来使这些事务成功并发运行吗?对我来说很明显,快照隔离的冲突检测导致了此错误,但是由于这里没有实际的冲突,我想知道我们是否可以通过某种提示或设置来解决它。

0 个答案:

没有答案