T-SQL 中的悲观锁 [英] Pessimistic lock in T-SQL

查看:33
本文介绍了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 中的悲观锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆