SQL Server, the misleading XLOCK & optimizations

后端 未结 3 1460
面向向阳花
面向向阳花 2020-12-08 16:39

From some recent testing and reading I\'ve done, it seems the \"X\" (exclusive) name part of XLOCK is misleading. It in fact doesn\'t lock any more than UPDLOCK. If it were

3条回答
  •  暖寄归人
    2020-12-08 16:40

    It's not a caveat, it's a misunderstanding on what happens in SELECT.

    A mere SELECT does not asks for Shared locks if the pages do not contain dirty data, and thus is not blocked by XLOCK.

    To be blocked by XLOCK, you need to run in REPEATABLE READ isolation level. Two things can trigger that:

    1. Modifying data, through INSERT/UPDATE/DELETE. The table updated does not have to be the one the XLOCK is on.
    2. Explicitly asking for REPEATABLE READ through transaction isolation level or table hint.

提交回复
热议问题