【发布时间】:2021-12-12 12:26:25
【问题描述】:
我试图弄清楚如果有一个给定名称的锁,如何查询 SQL Server,也就是有一个锁。
这是否正确,因为它似乎过于复杂?
跑步
begin tran
print 'APPLOCK_TEST=' + convert(varchar(max), APPLOCK_TEST('public', 'testlock', 'Exclusive', 'Session'))
print 'APPLOCK_MODE=' + APPLOCK_MODE('public', 'testlock', 'Session')
declare @lock int
exec @lock = sp_getapplock @Resource = 'testlock', @Lockmode = 'Exclusive', @LockOwner = 'Session'
print(@lock)
print 'APPLOCK_TEST=' + convert(varchar(max), APPLOCK_TEST('public', 'testlock', 'Exclusive', 'Session'))
print 'APPLOCK_MODE=' + APPLOCK_MODE('public', 'testlock', 'Session')
返回
APPLOCK_TEST=1
APPLOCK_MODE=NoLock
0
APPLOCK_TEST=1
APPLOCK_MODE=Exclusive
在另一个选项卡中运行:
begin tran
print 'APPLOCK_TEST=' + convert(varchar(max), APPLOCK_TEST('public', 'testlock', 'Exclusive', 'Session'))
print 'APPLOCK_MODE=' + APPLOCK_MODE('public', 'testlock', 'Session')
返回
APPLOCK_TEST=0
APPLOCK_MODE=NoLock
那么检查是否持有锁的方法是什么?
进行这种检查的方法看起来很奇怪。
//APPLOCK_TEST remains 1 when the session is the session who has the lock
//otherwise this is 0
//APPLOCK_MODE is "NoLock" when a different session has the lock
//otherwise if its the same then APPLOCK_MODE = "Exclusive"
if(APPLOCK_TEST == 0 or APPLOCK_MODE != 'NoLock')
{
//there currently is a lock
}
else
{
//there is no lock.
}
我一直在看
https://www.mssqltips.com/sqlservertip/3202/prevent-multiple-users-from-running-the-same-sql-server-stored-procedure-at-the-same-time/ https://github.com/madelson/DistributedLock/blob/5bd69af3f151710825f43ea1379a619a1b63ca1a/DistributedLock.SqlServer/SqlApplicationLock.cs#L164 https://www.endyourif.com/preventing-race-conditions-with-sp_getapplock/
还有更多..
请协助 SQL Server 检查当前是否存在锁,无论使用何种连接。
【问题讨论】:
-
你还没有提交第一个查询的事务,你期望什么?
-
@Charlieface 请给我看代码,我试试看,结果完全一样......这也是我想的那样,但我想我会检查......
-
COMMIT TRAN;在第一个脚本的末尾,在运行第二个脚本之前 -
我认为您的问题是如何确定当前会话是否已经对指定资源具有相同的锁定。带有会话锁的事务是多余的。
-
@Charlieface 就像我说的那样不会改变结果......
标签: sql-server locking distributed-lock