编程语言
首页 > 编程语言> > C#中的交易并发

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

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

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

请帮我.

解决方法:

支持不观察锁的读取,因为最坏的情况是,您会向请求nolock的SPID引起数据完整性问题(皮顿/不可重复的读取)-很好:这是自我造成的.

AFAIK不支持不观察锁的写入.因为这将使您引起其他SPID的数据完整性问题.那绝对不是很好.

所以基本上据我所知:您不能.您必须等待获取锁.

避免锁延迟的最好方法是确保事务执行确保一致性更改所需的最少工作(并且在事务中间无需外部操作).

标签:concurrency,transactions,nolock,sql,c
来源: https://codeday.me/bug/20191201/2084598.html