home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.databases.ms-sqlserver      Notorious Rube Goldberg contraption      19,505 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 19,250 of 19,505   
   Erland Sommarskog to Anton Shepelev   
   Re: Using sp_getapplock outside a transa   
   25 Nov 17 21:36:06   
   
   From: esquel@sommarskog.se   
      
   Anton Shepelev (anton.txt@gmail.com) writes:   
   > 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.   
   >   
      
   As RJA says, you need to specify LockOwner to sp_releaseapplock as well.   
      
   And you don't need the transaction. This runs successfully on my machine:   
      
   DECLARE @getRes INT   
   DECLARE @resName VARCHAR(16) = 'test'   
     EXEC @getRes = sp_getapplock @resName, 'Exclusive', @LockOwner = 'Session'   
     IF @getRes >= 0   
     BEGIN   
       PRINT 'Lock acquired successfully. Releasing...'   
       EXEC sp_releaseapplock @resName, 'Session'   
     END   
     ELSE PRINT 'Failed to acquire lock'   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca