SQL Server 和可能的竞争条件

发布于 2024-12-15 09:33:38 字数 324 浏览 5 评论 0原文

假设我使用 ASP.NET 编写一个使用 SQL Server 数据库的网站。我的脚本执行如下操作:

-- The meaning of the SQL doesn't matter
UPDATE t1 SET [c1]=1 WHERE [id]=3;
UPDATE t1 SET [c2]=0 WHERE [id]=4;
UPDATE t1 SET [c1]=2 WHERE [id]=7;

当上面的 SQL 运行时,另一个用户登录到同一页面并同时运行相同的 SQL 脚本。 SQL Server 是否确保多个同时执行的 SQL 命令不会混合在一起并导致竞争条件?

Say, if I write a web site using ASP.NET that uses the SQL Server database. My script executes something like this:

-- The meaning of the SQL doesn't matter
UPDATE t1 SET [c1]=1 WHERE [id]=3;
UPDATE t1 SET [c2]=0 WHERE [id]=4;
UPDATE t1 SET [c1]=2 WHERE [id]=7;

And while the SQL above is running another user logs in to the same page and runs the same SQL script at the same time. Does SQL Server ensure that multiple simultaneous SQL execute commands won't get mixed together and cause a race condition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

好久不见√ 2024-12-22 09:33:38

不,除非您包装交易:

BEGIN TRANSACTION
  UPDATE t1 SET [c1]=1 WHERE [id]=3; 
  UPDATE t1 SET [c2]=0 WHERE [id]=4; 
  UPDATE t1 SET [c1]=2 WHERE [id]=7; 
COMMIT

No, not unless you wrap in a transaction:

BEGIN TRANSACTION
  UPDATE t1 SET [c1]=1 WHERE [id]=3; 
  UPDATE t1 SET [c2]=0 WHERE [id]=4; 
  UPDATE t1 SET [c1]=2 WHERE [id]=7; 
COMMIT
潇烟暮雨 2024-12-22 09:33:38

除非你明确锁定桌子,否则我不会这么认为。

Unless you explicetly lock the tables I won't think so.

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