C# 中的事务并发

发布于 2024-12-22 20:15:59 字数 926 浏览 2 评论 0原文

用户 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-12-29 20:15:59

支持在不观察锁的情况下进行读取,因为最坏的情况是您会导致请求 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文