select语句上的sql行锁

发布于 2024-08-29 16:37:08 字数 106 浏览 4 评论 0原文

我有一个 ASP.Net 网页,用户在其中选择一行进行编辑。我想在该行上使用行锁,一旦用户完成编辑并更新,另一个用户就可以编辑该行,即如何使用行锁以便只有一个用户可以编辑一行?

谢谢

I have an ASP.Net webpage where the user selects a row for editing. I want to use the row lock on that row and once the user finishes the editing and updates another user can edit that row i.e. How can I use rowlock so that only one user can edit a row?

Thank you

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

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

发布评论

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

评论(2

情释 2024-09-05 16:37:08

您无法使用数据库引擎锁来锁定这样的行。

大多数其他策略将依赖于保持连接打开(例如 sp_getapplock),这在 Web 应用程序中是无意义的。

即使您在该行上设置了一个标志,如果用户只是在编辑过程中关闭浏览器,会发生什么?

我建议使用时间戳/行版本列来检测其他会话中行的更改。

You can't lock a row like that using DB engine locks.

Most other strategies would rely on keeping the connection open (such as sp_getapplock) and this is nonsensical in web apps.

Even if you set a flag on the row, what happens if the user simply closes the browser mid-edit?

I'd suggest using a timestamp/rowversion column to detect changes to the row in other sessions.

挖个坑埋了你 2024-09-05 16:37:08

这里我在 select 语句期间给了两组 sql 行锁。

BEGIN TRAN

SELECT *
FROM   authors AU
WITH   (HOLDLOCK, ROWLOCK)
WHERE  AU.au_id = '274-80-9391'

/* Do all your stuff here while the row is locked */

COMMIT TRAN

HOLDLOCK 提示礼貌地要求 SQL Server 保持锁定,直到您提交事务。 ROWLOCK 提示礼貌地要求 SQL Server 仅锁定该行,而不是发出页锁或表锁。

请注意,如果大量行受到影响,SQL Server 要么会采取主动并升级为页锁,要么您将有一整群行锁填满服务器的内存并导致处理陷入困境。

SELECT id From mytable WITH (ROWLOCK, UPDLOCK) WHERE id = 1 

我想与您分享另一个好的链接 https://www.mssqltips.com/sqlservertip/1257/processing-data-queues-in-sql-server-with-readpast-and-updlock/

谢谢

Here i am giving two set of sql to row lock during select statement.

BEGIN TRAN

SELECT *
FROM   authors AU
WITH   (HOLDLOCK, ROWLOCK)
WHERE  AU.au_id = '274-80-9391'

/* Do all your stuff here while the row is locked */

COMMIT TRAN

The HOLDLOCK hint politely asks SQL Server to hold the lock until you commit the transaction. The ROWLOCK hint politely asks SQL Server to lock only this row rather than issuing a page or table lock.

Be aware that if lots of rows are affected, either SQL Server will take the initiative and escalate to page locks, or you'll have a whole army of row locks filling your server's memory and bogging down processing.

another one

SELECT id From mytable WITH (ROWLOCK, UPDLOCK) WHERE id = 1 

another good link i want to share with you on lock. https://www.mssqltips.com/sqlservertip/1257/processing-data-queues-in-sql-server-with-readpast-and-updlock/

thanks

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