如何在不更新记录的情况下知道行的锁定状态 [英] How to know the lock status for a row, without updating the record

查看:66
本文介绍了如何在不更新记录的情况下知道行的锁定状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用重新提交隔离级别来更新一个事务中的一行.从其他事务中,我应该如何找到在同一行上放置的锁,而无需尝试更新该行. 有什么主意吗?

I am using the readcommited isolation level to update a row in one transaction. From other transaction, How should I find there is a lock placed on the same row without trying to update the row. Any idea?

推荐答案

检查锁的唯一方法是获取自己的锁.这不是特定于数据库的,并发是一个普遍的问题.从根本上讲,检查"是否持有锁的任何形式的API都会被破坏,因为根据定义,基于此API检查结果执行的任何操作都是错误,这是基础过时的,过时的信息.知道在检查API时未持有锁 并不意味着在返回API时未持有锁.

The only way to check for a lock is by obtaining the lock your self. This is not specific to databases, is a general issue with concurrency. Any form of API that 'checks' if a lock is held or not is fundamentally broken because by definition any action performed based on the result of this API check is wrong, as is base don stale, obsolete information. Knowing that lock was not held when the API checked for it does not mean is not held by the time the API returned.

如果您查询有关锁的信息,则可能无法编写正确的程序.编写正确程序的唯一方法是 acquire 锁.与其他任何编程语言一样,在SQL中也是如此.

You cannot possibly write a correct program if you inquire about locks. the only way to write a correct program is to acquire locks. True in SQL as in any other programming language.

没有什么可以阻止您尝试获取即时超时的锁( SET LOCK_TIMEOUT 0 )并处理发生冲突时发生的锁定超时错误.

Nothing stops you from attempting to acquire a lock with instant timeout (SET LOCK_TIMEOUT 0) and handle the lock timeout error that occurs on conflict.

这篇关于如何在不更新记录的情况下知道行的锁定状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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