Minimum transaction isolation level to avoid “Lost Updates”

后端 未结 6 948
攒了一身酷
攒了一身酷 2020-12-30 02:57

With SQL Server\'s transaction isolation levels, you can avoid certain unwanted concurrency issues, like dirty reads and so forth.

The one I\'m interested in right n

6条回答
  •  萌比男神i
    2020-12-30 03:36

    The example in the book is of Clerk A and Clerk B receiving shipments of Widgets.

    They both check the current inventory, see 25 is in stock. Clerk A has 50 widgets and updates to 75, Clerk B has 20 widgets and so updates to 45 overwriting the previous update.

    I assume she meant this phenomena can be avoided at all isolation levels by Clerk A doing

    UPDATE Widgets
    SET StockLevel = StockLevel + 50
    WHERE ...
    

    and Clerk B doing

    UPDATE Widgets
    SET StockLevel = StockLevel + 20
    WHERE ...
    

    Certainly if the SELECT and UPDATE are done as separate operations you would need repeatable read to avoid this so the S lock on the row is held for the duration of the transaction (which would lead to deadlock in this scenario)

提交回复
热议问题