Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sp_getapplock without transaction

I'm implementing stored procedure that won't have transactions inside. Actually, it will, but only on specific spots to reduce time to minimum. Nature of stored procedure is so I only want one runinng at a time.

I tried to use sp_getapplock but quickly found that it needs to be inside transaction.

Is there any other alternative where I can place lock on whole procedure but without wrapping it into transaction?

like image 894
katit Avatar asked Jul 17 '12 23:07

katit


1 Answers

pass in Session via @LockOwner to get a Session-scope lock; this does not need a transaction to be alive when lock is taken.

For example

EXEC @res = sp_getapplock @Resource = 'Lock ID', @LockOwner = 'Session', @LockMode = 'Exclusive';
..
code
..

EXEC @res = sp_releaseapplock @Resource = 'Lock ID';
like image 118
iruvar Avatar answered Sep 22 '22 11:09

iruvar