Bab*_*ish 4 c# deadlock stored-procedures sql-server-2008 linq-to-sql
我有一个使用linq死锁的插入语句.所以我把它放在一个存储过程中,周围的语句正在影响它.
现在Stored Proc已经死锁了.关于insert语句的一些内容是根据Server Profiler锁定自身.它声称其中两个插入语句正在等待释放PK索引:
当我将代码放入存储过程时,它现在声明此存储过程与此存储过程的另一个实例已死锁.
这是代码.select语句类似于linq在执行自己的查询时使用的语句.我只想查看该项是否存在,如果不存在则插入它.我可以通过PK或某些查找值找到系统.
SET NOCOUNT ON;
BEGIN TRY
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION SPFindContractMachine
DECLARE @id int;
set @id = (select [m].pkID from Machines as [m]
WHERE ([m].[fkContract] = @fkContract) AND ((
(CASE
WHEN @bByID = 1 THEN
(CASE
WHEN [m].[pkID] = @nMachineID THEN 1
WHEN NOT ([m].[pkID] = @nMachineID) THEN 0
ELSE NULL
END)
ELSE
(CASE
WHEN ([m].[iA_Metric] = @lA) AND ([m].[iB_Metric] = @lB) AND ([m].[iC_Metric] = @lC) THEN 1
WHEN NOT (([m].[iA_Metric] = @lA) AND ([m].[iB_Metric] = @lB) AND ([m].[iC_Metric] = @lC)) THEN 0
ELSE NULL
END)
END)) = 1));
if (@id IS NULL)
begin
Insert into Machines(fkContract, iA_Metric, iB_Metric, iC_Metric, dteFirstAdded)
values (@fkContract, @lA, @lB, @lC, GETDATE());
set @id = SCOPE_IDENTITY();
end
COMMIT TRANSACTION SPFindContractMachine
return @id;
END TRY
BEGIN CATCH
if @@TRANCOUNT > 0
ROLLBACK TRANSACTION SPFindContractMachine
END CATCH
Run Code Online (Sandbox Code Playgroud)
遵循该模式的任何过程:
BEGIN TRAN
check if row exists with SELECT
if row doesn't exist INSERT
COMMIT
Run Code Online (Sandbox Code Playgroud)
在生产中会遇到麻烦,因为没有什么可以阻止两个踏板同时进行检查,并且两者都得出应该插入的结论.特别是,在序列化隔离级别(如您的情况下),此模式保证死锁.
一个多更好的方式是使用数据库中是唯一的约束,总是插入,捕获重复键冲突的错误.这也显着提高了性能.
另一种方法是使用MERGE语句:
create procedure usp_getOrCreateByMachineID
@nMachineId int output,
@fkContract int,
@lA int,
@lB int,
@lC int,
@id int output
as
begin
declare @idTable table (id int not null);
merge Machines as target
using (values (@nMachineID, @fkContract, @lA, @lB, @lC, GETDATE()))
as source (MachineID, ContractID, lA, lB, lC, dteFirstAdded)
on (source.MachineID = target.MachineID)
when matched then
update set @id = target.MachineID
when not matched then
insert (ContractID, iA_Metric, iB_Metric, iC_Metric, dteFirstAdded)
values (source.contractID, source.lA, source.lB, source.lC, source.dteFirstAdded)
output inserted.MachineID into @idTable;
select @id = id from @idTable;
end
go
create procedure usp_getOrCreateByMetrics
@nMachineId int output,
@fkContract int,
@lA int,
@lB int,
@lC int,
@id int output
as
begin
declare @idTable table (id int not null);
merge Machines as target
using (values (@nMachineID, @fkContract, @lA, @lB, @lC, GETDATE()))
as source (MachineID, ContractID, lA, lB, lC, dteFirstAdded)
on (target.iA_Metric = source.lA
and target.iB_Metric = source.lB
and target.iC_Metric = source.lC)
when matched then
update set @id = target.MachineID
when not matched then
insert (ContractID, iA_Metric, iB_Metric, iC_Metric, dteFirstAdded)
values (source.contractID, source.lA, source.lB, source.lC, source.dteFirstAdded)
output inserted.MachineID into @idTable;
select @id = id from @idTable;
end
go
Run Code Online (Sandbox Code Playgroud)
此示例将两种情况分开,因为T-SQL查询永远不应尝试在一个查询中解析两个不同的解决方案(结果永远不可优化).由于手头的两个任务(通过mahcine id和get by metrics)是完全独立的,因此应该是单独的过程,调用者应该调用apropiate,而不是传递一个标志.这个例子说明如何使用MERGE实现(可能)期望的结果,但当然,正确和最佳的解决方案取决于实际的模式(表定义,索引和适当的指标)和实际要求(不清楚是什么如果标准已经匹配,而不是输出和@id,那么程序应该做.
通过消除SERIALIZABLE隔离,不再保证死锁,但它仍可能死锁.当然,解决死锁完全取决于未指定的模式,因此在此上下文中实际上无法提供死锁的解决方案.有一个锁定所有候选行的大锤(强制UPDLOCK甚至TABLOCX),但是这样的解决方案会在大量使用时扼杀吞吐量,因此我不能在不知道用例的情况下推荐它.
归档时间: |
|
查看次数: |
473 次 |
最近记录: |