简体   繁体   English

为什么TABLOCKX没有死锁

[英]why TABLOCKX does not have deadlock

I need help to understand why tablockx does have deadlock in multithreaded application. 我需要帮助来理解为什么tablockx在多线程应用程序中确实存在死锁。

I tried tablockx with SqlQueryStress will multiple threads still i am not getting any deadlock 我尝试使用SqlQueryStress tablockx将多线程仍然没有任何死锁

Because deadlocks can pretty much only occur when you use row-level locking. 因为死锁几乎只会在使用行级锁定时发生。 The most basic example of a deadlock is: 死锁最基本的例子是:

  • Transaction A is updating Rows 1,2,3 交易A正在更新第1,2,3行
  • Transaction B is updating Rows 3,2,1 交易B正在更新第3,2,1行
  • A modifies row 1, opens (locks) row 2, and waits for row 3 A修改第1行,打开(锁定)第2行,然后等待第3行
  • B locks row 3, and waits for row 2 B锁定第3行,并等待第2行

Since A has 2 and needs 3, and B has 3 and needs 2, they will wait forever unless the query engine catches it, which is what causes a deadlock. 由于A有2并且需要3,而B有3并需要2,它们将永远等待,除非查询引擎捕获它,这是导致死锁的原因。 Basically it's an infinite logical loop of waits that get cancelled. 基本上它是一个被取消的等待的无限逻辑循环。

When you use a TABLOCK , which indicates to the engine to use table locking instead of row locking, Transaction A would either wait for the whole table to be available if B already had it open, or would place a lock on the whole table if it were available, blocking any other transaction from modifying the table until it is complete. 当你使用TABLOCK ,它指示引擎使用表锁定而不是行锁定时,如果B已经打开它,事务A将等待整个表可用,或者如果它已经锁定整个表可用,阻止任何其他事务修改表,直到它完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM