Mutex Stored Procedure

后端 未结 1 1439
野性不改
野性不改 2021-01-14 06:36

I want to create some distributed mutual exclusion using a database table. It would be nice to have the following interface on a stored procedure:

Wait(uniqueidentif

相关标签:
1条回答
  • 2021-01-14 07:28

    Take a look at the system stored procedure:

     sp_getapplock
    

    It may help you accomplish what you are trying to do.

    http://msdn.microsoft.com/en-us/library/ms189823.aspx

    You can put it in a proc that parametrizes the uniqueidentifier...

    BEGIN TRAN
    
    DECLARE @result int
    
    EXEC @result = sp_getapplock @Resource = 'YOUR_uniqueidentifier_HERE', 
                                 @LockMode = 'Exclusive',
                                 @LockTimeout = 90
    
    IF @result NOT IN ( 0, 1 )   -- Only successful return codes
    BEGIN
      PRINT @result
      RAISERROR ( 'Lock failed to acquire...', 16, 1 )
    END 
    ELSE
    BEGIN
        -- DO STUFF HERE 
    END
    EXEC @result = sp_releaseapplock @Resource = 'YOUR_uniqueidentifier_HERE'  
    
    COMMIT TRAN
    
    0 讨论(0)
提交回复
热议问题