SQL Server deadlocks between select/update or multiple selects

后端 未结 5 1500
一生所求
一生所求 2020-12-23 19:57

All of the documentation on SQL Server deadlocks talks about the scenario in which operation 1 locks resource A then attempts to access resource B and operation 2 locks reso

5条回答
  •  隐瞒了意图╮
    2020-12-23 20:54

    I'm surprised no one has mentioned the WITH (UPDLOCK) locking hint. It's very useful if you have deadlocks involving e.g. two select-insert pairs running in parallel.

    In SQL Server, if you issue the selects with WITH (UPDLOCK), the second select will wait until the first select is finished. Otherwise they get shared locks, and when they simultaneously try to upgrade to exclusive locks, they deadlock.

提交回复
热议问题