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 中的悲观锁


基础教程推荐
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01