gpt4 book ai didi

php - LOCK TABLES 会导致 InnoDB 中的死锁吗?

转载 作者:行者123 更新时间:2023-11-29 07:26:25 25 4
gpt4 key购买 nike

来自 MySQL 手册(https://dev.mysql.com/doc/refman/8.0/en/innodb-deadlocks.html):

To reduce the possibility of deadlocks, use transactions rather than LOCK TABLES statements

在 InnoDB 中使用 LOCK TABLES 如何导致死锁?

例如,如果我写

SET autocommit=0;
LOCK TABLES t1 WRITE, t2 WRITE, t3 WRITE, t4 WRITE;
... do something with tables t1-t4 here ...
COMMIT;
UNLOCK TABLES;

每次执行此脚本时我真的必须检查 1213 之类的错误吗?

最佳答案

如果您确保在一个 LOCK TABLES 语句中锁定所有您将读取或写入的表,您应该能够避免死锁。

如果可以使用事务来避免使用 LOCK TABLES 的另一个很好的理由是允许行级锁定。 LOCK TABLES 仅在表级别锁定,这意味着并发 session 不能触及表中的任何行,即使您的 session 不需要锁定它们。

这对于需要允许多个 session 同时访问表的软件来说是一个缺点。您正在强制执行表级锁定,这将限制您的软件吞吐量,因为访问表的所有 session 将相互排队,并被迫串行执行。

What do you mean by "use t2"? A READ lock? What if I'm using only WRITE locks like in my example.

我认为他的意思是如果您从表 t2 中读取。由于您为 WRITE 锁定了该表,这也包括阻止该表的任何读者。在您解锁之前,没有其他 session 可以读取或写入该表。

I'm not concerned about performance. I have a situation where I want to make things as simple as possible and LOCK TABLES feels much more intuitive to me than using transactions with paranoid level error checking.

您最终会发现一个案例,您希望您的软件具有良好的性能。您必须更加习惯使用事务。

关于php - LOCK TABLES 会导致 InnoDB 中的死锁吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53483794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com