【问题标题】:Transaction Concurrency in C#C# 中的事务并发
【发布时间】:2012-01-27 16:13:04
【问题描述】:

用户 1:

begin tran
select * from items with(nolock);
insert into orders (odate) values(getdate());

insert into OrderData values((select max(orderid) from Orders with(nolock)),1,1);
update Items set qih=qih-1 where item_id=1;
select * from OrderData where oid=(select max(orderid) from Orders with(nolock));

insert into OrderData values((select max(orderid) from Orders with(nolock)),2,1);
update Items set qih=qih-1 where item_id=2;
select * from OrderData where oid=(select max(orderid) from Orders with(nolock));

commit tran;

用户 2:

begin tran
select * from items with(nolock);
insert into orders (odate) values(getdate());

insert into OrderData values((select max(orderid) from Orders with(nolock)),1,1);//in here waiting this user

在提交 user1 之后。用户 2 的最后一条语句正在执行。

但我想执行此用户 2 的最后一条语句而不是等待。我该怎么做。

请帮帮我。

【问题讨论】:

    标签: c# sql concurrency transactions nolock


    【解决方案1】:

    在不观察锁的情况下读取是可以支持的,因为最坏的情况是您对请求 nolock 的 SPID 造成数据完整性问题(幻顿/不可重复读取) - 这很好:这是自己造成的。

    不支持不观察锁的写入,AFAIK,不支持。因为这会让您对其他 SPID 造成数据完整性问题。这绝对是好的。

    基本上是这样;据我所知:你不能。您必须等待才能获得锁。

    避免锁定延迟的最佳方法是确保事务做最少的工作以确保一致的更改(并且在事务中间没有外部操作)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 2016-11-03
      • 2014-10-17
      • 2011-10-18
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多