没有事务的 sp_getapplock [英] sp_getapplock without transaction
问题描述
我正在实现内部没有事务的存储过程.实际上,它会,但只能在特定地点将时间减少到最少.存储过程的性质是我一次只想要一个运行.
I'm implementing stored procedure that won't have transactions inside. Actually, it will, but only on specific spots to reduce time to minimum. Nature of stored procedure is so I only want one runinng at a time.
我尝试使用 sp_getapplock 但很快发现它需要在事务内部.
I tried to use sp_getapplock but quickly found that it needs to be inside transaction.
有没有其他替代方法可以让我锁定整个过程而不将其包装到事务中?
Is there any other alternative where I can place lock on whole procedure but without wrapping it into transaction?
推荐答案
通过@LockOwner 传入Session,获取Session-scope锁;这不需要在获取锁时事务处于活动状态.
pass in Session via @LockOwner to get a Session-scope lock; this does not need a transaction to be alive when lock is taken.
例如
EXEC @res = sp_getapplock @Resource = 'Lock ID', @LockOwner = 'Session', @LockMode = 'Exclusive';
..
code
..
EXEC @res = sp_releaseapplock @Resource = 'Lock ID';
这篇关于没有事务的 sp_getapplock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!