SQL 2005 sp_GetAppLock --- When to call sp_ReleaseAppLock?

Sorry for the last post. I made a paste over my question. In short, when using sp_GetAppLock inside a try / catch block, should I call sp_ReleaseAppLock when the exception is caught?


SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

BEGIN TRAN 

DECLARE @res INT

EXEC @res = sp_getapplock
           @Resource = 'This a Lock ID 3',
           @LockMode = 'Exclusive', 
           @LockOwner = 'Transaction', 
           @LockTimeout = 60000,
           @DbPrincipal = 'public'

if @res < 0
begin
    declare @errorMessage nvarchar(200)

    set @errorMessage =
             case @res
                 when -1 then 'Applock request timed out.'
                 when -2 then 'Applock request canceled.'
                 when -3 then 'Applock involved in deadlock'
                  else 'Parameter validation or other call error.'endraiserror (@errorMessage,16,1)
             end

 SELECT...
 INSERT...
 UPDATE...

COMMIT TRANSACTION -- THIS RELEASES THE APPLOCK 

RETURN 0; END TRY

BEGIN CATCH

-- ROLLBACK TRANSACTION IF NEEDED IF @@TRANCOUNT > 0 ROLLBACK

/* Exception handling stuff here. Should I call sp_releaseapplock? ... ... */

-- return the success code RETURN -1; 

END CATCH
+5
source share
2 answers

From sp_getapplock

Locks associated with the current transaction are released when the transaction commits or rolls back.

So this is not necessary because you are rolling back.

, , CATCH APPLOCK_TEST. FINALLY, .

, . , ( ), COMMIT/ROLLBACK, . , , CATCH, ...

+5
+1

All Articles