Sorry about the last message. I did a paste over my question. Long question short, when using sp_GetAppLock inside of a try / catch block, should I call sp_ReleaseAppLock when an 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