Is UPDATE command thread safe (tracking revisions) in MS SQL

前端 未结 1 759
一向
一向 2021-01-23 07:19

Suppose we have a blog tool where each time a user performs a modification on an Article(Id,Body,Revisions), the revision counter is incremented by 1. If we wou

相关标签:
1条回答
  • 2021-01-23 08:18

    Yes, this is thread-safe. The database engine will lock the record during the update, which means any other threads will have to wait for it to finish its update.

    During that time the field will indeed increment with one, without any interference from other threads. Once done, the resource is unlocked, and the next waiting thread will lock it in turn, and do the same.

    As explained in the docs, the lock is an exclusive one:

    Exclusive (X) Used for data-modification operations, such as INSERT, UPDATE, or DELETE. Ensures that multiple updates cannot be made to the same resource at the same time.

    and:

    Exclusive Locks
    Exclusive (X) locks prevent access to a resource by concurrent transactions. No other transactions can read or modify data locked with an exclusive (X) lock.

    0 讨论(0)
提交回复
热议问题