Detecting dirty reads from a stored procedure

前端 未结 8 536
自闭症患者
自闭症患者 2021-02-04 02:27

I\'ve got 100 threads that are each calling the stored procedure as defined below.

How do I prevent dirty reads?

SET QUOTED_IDENTIFIER OFF
SET ANSI_NULLS         


        
8条回答
  •  悲哀的现实
    2021-02-04 03:02

    One option would be to use the sp_getapplock system stored procedure and use the built in locking of sql server to ensure serialized access to a resource.

    CREATE PROC MyCriticalWork(@MyParam INT)      
    AS
        DECLARE @LockRequestResult INT
        SET @LockRequestResult=0
    
        DECLARE @MyTimeoutMiliseconds INT
        SET @MyTimeoutMiliseconds=5000--Wait only five seconds max then timeouit
    
        BEGIN TRAN
    
        EXEC @LockRequestResult=SP_GETAPPLOCK 'MyCriticalWork','Exclusive','Transaction',@MyTimeoutMiliseconds
        IF(@LockRequestResult>=0)BEGIN
    
            /*
            DO YOUR CRITICAL READS AND WRITES HERE
            */
    
            --Release the lock
            COMMIT TRAN
        END ELSE
            ROLLBACK TRAN   
    

提交回复
热议问题