简体   繁体   English

已超过READ COMMITTED锁定等待超时

[英]READ COMMITTED lock wait timeout exceeded

I have one question regarding transaction isolation level and locking in mysql. 我有一个关于事务隔离级别和mysql锁定的问题。

I have one query keeps getting the lock timeout error: 我有一个查询不断收到锁定超时错误:

java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction

The queries basically try to delete records with a specific date_id. 这些查询基本上尝试删除具有特定date_id的记录。 BTW, the date_id is indexed as well. 顺便说一句,date_id也被索引了。 But most of the times, there are no records match, ie it will delete nothing. 但是在大多数情况下,没有记录匹配,即它不会删除任何内容。

After some investigation, I found it may be caused by a culprit long-running query, which do a range select on the same table. 经过一番调查,我发现这可能是由于长期运行查询的原因,该查询在同一张表上进行了范围选择。 However, what confused me is both transaction run at isolation level of "READ COMMITTED". 但是,令我困惑的是这两个事务都在隔离级别“ READ COMMITTED”下运行。 So I have no clue why a lock is needed and why it could timeout (especially consider there is no matching record to delete) 所以我不知道为什么需要一个锁以及为什么它可能超时(尤其是考虑到没有要删除的匹配记录)

Thank you in advance! 先感谢您!

The transaction isolation "read committed" is a contract: the database promises to only read committed data and keep not-yet-committed data out of the transaction. 事务隔离“读取已提交”是一个契约:数据库承诺仅读取已提交的数据,并将尚未提交的数据排除在事务之外。 The lock timeout error is a runtime-error: the database tries to update data but it cannot find a good moment to do so (see the "innodb_lock_wait_timeout" mentioned here in the MySQL reference manual). 锁定超时错误是运行时错误:数据库尝试更新数据,但找不到合适的时机(请参见MySQL参考手册中此处提到的“ innodb_lock_wait_timeout”)。 Even if there is no data to delete, the database needs to find a moment in time to assert that. 即使没有要删除的数据,数据库也需要及时找到一个断言。

The transaction isolation "read committed" already improves the database's chances of finding a good moment to update data (see here for example), but it cannot prevent other queries/transactions from locking the entire table ("full table scan" like your culprit query probably does). 事务隔离“已提交读”已经提高了数据库寻找更新数据的好机会(例如,请参见此处 ),但是它不能防止其他查询/事务锁定整个表(像“罪魁祸首”查询那样锁定“全表扫描”)可能会)。

Some more searching does show a possible solution for your delete problem. 更多的搜索确实显示了删除问题的可能解决方案

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

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