T-SQL 中的悲观锁

Pessimistic lock in T-SQL(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 中的悲观锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)