How do UPDATE locks prevent a common form of deadlock?

。_饼干妹妹 提交于 2019-12-04 06:33:07

The description is incomplete. If you look at the phrase "only one transaction can obtain an update (U) lock to a resource at a time". That is no different from an exclusive (X) lock - only one transaction can obtain an exclusive (X) lock to a resource at at time.

U-locks are compatible with S-locks which is not the case for X-locks. This means that while the rows to be written to are determined (using U-locks), other readers are still allowed.

So now add a second process...

The misunderstanding here is, that writers upgrade from S to U. This is not the case. They use U from the start. They upgrade from U to X later, but that has no meaning regarding deadlocks in this case.

To make this more clear: Let's assume we run the following statement:

UPDATE T SET SomeCol = 1 WHERE (ID BETWEEN 1 AND 2) AND (SomeOtherCond = 1)

Assume, that this is executed using a range scan on the clustered index on ID, and that SomeOtherCond = 1 is only true for the row ID = 2. This will get you U-locks for both rows, and an upgrade to X for the row with ID = 2. The U-lock for row ID = 2 will be released early.

An update lock is used to find rows that are to be updated, so are used by DELETE and UPDATE statements. Once rows are found for Update then the lock is converted to an X lock.

See Kalen Delany Blog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!