rja.carnegie to Anton Shepelev:
> > must be executed from within a transaction.
> >
> > When I try, however, to get a lock owned by the
> > session outside a transaction:
> >
> > EXEC sp_getapplock 'test', 'Exclusive', @LockOwner = 'Session'
> > EXEC sp_releaseapplock 'test'
> >
> > it fails with:
> >
> > The statement or function must be executed in
> > the context of a user transaction.
> >
> > Does not this behavor it contradict the documen-
> > tation?
>
> I don't know why it isn't working, but it may be
> permissions -- in which case the error message is
> not a very good message.
Does not seem to be permissions, for I am testing it
with full rights as 'sa'. Furhtermore, the return
value indicates the lock has been acquired. When I
execute:
DECLARE @getRes INT
DECLARE @resName VARCHAR(16) = 'test'
BEGIN TRAN
EXEC @getRes = sp_getapplock @resName, 'Exclusive', @LockOwner = 'Session'
IF @getRes >= 0
BEGIN
PRINT 'Lock acquired successfully. Releasing...'
EXEC sp_releaseapplock @resName
END
ELSE PRINT 'Failed to acquire lock'
ROLLBACK TRAN
I get the following surprising output:
Lock acquired successfully. Releasing...
Msg 1223, Level 16, State 1, Procedure xp_userlock, Line 1
Cannot release the application lock (Database Principal:
'public', Resource: 'test') because it is not currently held.
--
Anton Shepelev