Detecting dirty reads from a stored procedure

前端 未结 8 520
自闭症患者
自闭症患者 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:01

    As already mentioned, you can use the auto increment built-in functionality, such as identity columns or sequence.

    If you don't want this, you need to make the access to the table in serial manner: using application lock or other abilities.

    For example, you can add the hints to the FIRST access to the table (in the transaction) like the below:

    UPDATE CfgCerealNumber
    Set CerealNumber = CerealNumber + 1
    FROM CfgCerealNumber with (tablockx, holdlock)
    WHERE CerealNumberID = @TableID
    

    This will guarantee the sequential access to the table in all parallel threads.

提交回复
热议问题