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

查看:79
本文介绍了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天全站免登陆