Pessimistic lock in T-SQL(T-SQL 中的悲观锁)
问题描述
如果我在 MS SQL Server 中选择一行进行更新,并希望将其锁定直到我更新或取消,哪个选项更好:-
If i SELECT a row for updating in MS SQL Server, and want to have it locked till i either update or cancel, which option is better :-
1) 使用像 UPDLOCK 这样的查询提示2) 对事务使用 REPEATABLE READ 隔离级别3) 任何其他选项.
1) Use a query hint like UPDLOCK 2) Use REPEATABLE READ isolation level for the transaction 3) any other option.
谢谢,查克.
推荐答案
两者都不是.当您的用户输入数据时,您几乎不想让事务保持打开状态.如果您必须实现这样的悲观锁,人们通常会通过滚动他们自己的功能来实现.
Neither. You almost never want to hold a transaction open while your user is inputting data. If you have to implement a pessimistic lock like this, people generally do it by rolling their own functionality.
考虑你正在做的事情的全部后果.我曾经在一个系统上工作过,它实现了这样的锁定.您经常会遇到大量陈旧的锁,当您强加给他们时,您的用户很快就会感到困惑和愤怒.在我们的案例中,我们的解决方案是完全删除此锁定功能.
Consider the full ramifications of what you are doing. I once worked on a system that implemented locking like this. You often run into tons of stale locks, and your users get confused and angry very quickly when you foist this on them. The solution for us in our case was to remove this locking functionality entirely.
这篇关于T-SQL 中的悲观锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:T-SQL 中的悲观锁


- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01