Question : Trouble with sp_getapplock

Hello,

I have a procedure that I need to serialize. To do this I am using sp_getapplock. The problem is the stored procedure works fine without the locking code. But once locking is introduced the procedure never completes. Locking code attached. Does anyone see what the hang up is?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
BEGIN TRANSACTION;
EXEC  @result = sp_getapplock @Resource = 'stp', 
     @LockTimeout = 0,
     @LockMode = 'Exclusive';
 
IF @result NOT IN ( 0, 1 )   -- Only successful return codes
	BEGIN
		PRINT @result
		RAISERROR ( 'Lock failed to acquire.', 16, 1 )
	END
	ELSE
       BEGIN
               /* previous code here */
 
               EXEC @result = sp_releaseapplock @Resource = 'stp';
               COMMIT TRANSACTION;
       END
Open in New Window Select All

Answer : Trouble with sp_getapplock

Its probably that you are refering to the object that you locked within the code. Since sql queries run as batches your query part that is putting lock and the the rest can be run under different sessions and cuases your sp to suck. Use transactions and read_uncommitted settings.
Regards
Random Solutions  
 
programming4us programming4us