我们最近发现我们的应用程序中存在我们开发人员认为不会发生的死锁问题。
为了进一步理解这一点,我着手创建一个我能想象到的基本测试场景。结果是两张桌子,一张家长和一张孩子。
在这个简单的场景中,我启动了 Management Studio 的两个实例,并在两个实例中同时(几乎,以我可以切换窗口的速度)同时执行了 SAME 查询。很快,其中一个以僵局告终。
我已经阅读了不同的方法并尝试启用 SNAPSHOT 隔离级别,但这并没有解决任何问题,死锁仍然存在。
我质疑插入如何导致死锁,并希望深入了解原因,并希望甚至提供解决问题的方法。
首先是简单的表格布局:
-- (Optional code to drop the tables and sequences)
drop table ChildTable
go
drop table ParentTable
go
drop sequence Seq_ParentSequence
drop sequence Seq_ChildSequence
create sequence Seq_ParentSequence as bigint start with 1 increment by 1 cache
create sequence Seq_ChildSequence as bigint start with 1 increment by 1 cache
-- Table creation
create table ParentTable (
ID bigint not null,
Name nvarchar(100),
CONSTRAINT [PK_ParentTable] PRIMARY KEY CLUSTERED
(
ID …Run Code Online (Sandbox Code Playgroud)