c#中的事务并发性



用户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

after commit user1。用户2的最后一条语句正在执行。

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

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

不观察锁的写是不支持的。因为这会导致其他 spid出现数据完整性问题。这绝对是而不是 OK

所以;据我所知,你不能。你得等一等才能拿到锁。

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

最新更新