最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

concurrency - Contradiction in MySQL manual regarding deadlocks - Stack Overflow

programmeradmin1浏览0评论

In the MySQL manual, in the section Deadlocks in InnoDB, it states:

The possibility of deadlocks is not affected by the isolation level, because the isolation level changes the behavior of read operations, while deadlocks occur because of write operations.

But this is contradicted in the sub-section How to Minimize and Handle Deadlocks, which says:

If you use locking reads...try using a lower isolation level such as READ COMMITTED.

So which is true? That deadlocks are not affected by the isolation level, or that a lower isolation level might help to minimize deadlocks?

I know the subject of RDBMS deadlocks is notoriously complicated and widely misunderstood. But having unclear information in the official docs doesn't help at all...

Would any MySQL experts be willing to clear up my confusion?

In the MySQL manual, in the section Deadlocks in InnoDB, it states:

The possibility of deadlocks is not affected by the isolation level, because the isolation level changes the behavior of read operations, while deadlocks occur because of write operations.

But this is contradicted in the sub-section How to Minimize and Handle Deadlocks, which says:

If you use locking reads...try using a lower isolation level such as READ COMMITTED.

So which is true? That deadlocks are not affected by the isolation level, or that a lower isolation level might help to minimize deadlocks?

I know the subject of RDBMS deadlocks is notoriously complicated and widely misunderstood. But having unclear information in the official docs doesn't help at all...

Would any MySQL experts be willing to clear up my confusion?

Share Improve this question edited Mar 13 at 5:04 Glenn Schmidt asked Mar 6 at 4:02 Glenn SchmidtGlenn Schmidt 6328 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

That's a great point out.
I think the possibility if deadlocks is certainly affected by transaction levels.

because the isolation level changes the behavior of read operations, while deadlocks occur because of write operations.

Is a categorically incorrect statement IMHO.
To give a counter example, BEGIN; UPDATE TABLE_A SET B=5; COMMIT; will take a gap-lock (given that column B is not a unique index.) if the isolation level is REPEATABLE_READ. However, if isolation level is READ_COMMITTED it would not take a gap lock https://dev.mysql/doc/refman/8.4/en/innodb-locking.html#innodb-gap-locks

Gap locking can be disabled explicitly. This occurs if you change the transaction isolation level to READ COMMITTED. In this case, gap locking is disabled for searches and index scans and is used only for foreign-key constraint checking and duplicate-key checking.

There are also other effects of using the READ COMMITTED isolation level. Record locks for nonmatching rows are released after MySQL has evaluated the WHERE condition. For UPDATE statements, InnoDB does a “semi-consistent” read, such that it returns the latest committed version to MySQL so that MySQL can determine whether the row matches the WHERE condition of the UPDATE.

Gap lock certainly does affect the possibility of deadlocks.

发布评论

评论列表(0)

  1. 暂无评论