sp_getapplock锁定返回的值是多少?

时间:2014-04-07 15:20:02

标签: sql-server-2012

我找到了这个例子,以避免在SP仍在运行时运行SP:

CREATE PROCEDURE some_sp AS

DECLARE @ret int,
        @lockresource sysname

SELECT @lockresource = object_name(@@procid)

BEGIN TRY
   EXEC @ret = sp_getapplock @lockresource, @LockMode = 'Exclusive',
                             @LockOwner = 'Session', @LockTimeout = 5

   IF @ret <> 0
   BEGIN
      RAISERROR ('Another instance of the procedure is already running, 16, 1)
      RETURN
   END

   -- Do stuff

   EXEC sp_releaseapplock @lockresource, 'Session'
END TRY
BEGIN CATCH
   IF @@trancount > 0 ROLLBACK TRANSACTIOn
   EXEC sp_releaseapplock @lockresource, 'Session'
END CATCH   

但我不确定这是不是我要找的。

如果SP正在运行,那么@ret的价值是什么?

EXEC @ret = sp_getapplock @lockresource, @LockMode = 'Exclusive',
                                 @LockOwner = 'Session', @LockTimeout = 5

如果它没有运行,它的价值是什么?

我已查看此page,但我不理解。

1 个答案:

答案 0 :(得分:2)

如果没有授予锁定,它将返回-1,等待5毫秒后锁定请求超时