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 最后一条语句而不是等待。我该怎么做呢。
请帮我。
user 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;
User 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. user 2 last statement is executeing.
But I want to execute this user 2 last statement not waiting. How do I do it.
Please Help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
支持在不观察锁的情况下进行读取,因为最坏的情况是您会导致请求 nolock 的 SPID 出现数据完整性问题(幻像/不可重复读取) - 这很好:它是自己造成的。
据我所知,不支持不观察锁的写入。因为这会导致其他 SPID 出现数据完整性问题。这绝对是不行的。
所以基本上;据我所知:你不能。你必须等待才能获得锁。
避免锁定延迟的最佳方法是确保事务执行最少的必要工作以确保一致的更改(并且在事务中间没有外部操作)。
Reading without observing locks is supportable, since the worst case is that you cause data integrity issues (phanton/non-repeatable reads) to the SPID that requested the
nolock
- which is fine: it was self-inflicted.Writing without observing locks is not, AFAIK, supported. Since this would allow you to cause data integrity issues to other SPIDs. And that is most definitely not OK.
So basically; to the best of my knowledge: you can't. You'll have to wait to get the lock.
The best way of avoiding delays on locks is to ensure that transactions do the minimum work necessary to ensure a coherent change (and without external operations in the middle of a transaction).