select语句上的sql行锁
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法使用数据库引擎锁来锁定这样的行。
大多数其他策略将依赖于保持连接打开(例如 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.
这里我在 select 语句期间给了两组 sql 行锁。
HOLDLOCK
提示礼貌地要求 SQL Server 保持锁定,直到您提交事务。ROWLOCK
提示礼貌地要求 SQL Server 仅锁定该行,而不是发出页锁或表锁。请注意,如果大量行受到影响,SQL Server 要么会采取主动并升级为页锁,要么您将有一整群行锁填满服务器的内存并导致处理陷入困境。
。
我想与您分享另一个好的链接
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.
The
HOLDLOCK
hint politely asks SQL Server to hold the lock until you commit the transaction. TheROWLOCK
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
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