表[表名]未锁定 [英] Table [tablename] is not locked
问题描述
我正在编写一个锁定表的MySQL查询:
I am writing a MySQL query that locks a table:
"LOCK TABLE table_1 WRITE"
此后,我正在执行一些功能,并且在其中一个功能中,我正在另一个未锁定的表上执行另一个查询:
After that i am executing some functions, and in one of those functions, I am executing another query, on another table that I haven't locked:
"SELECT * FROM completely_different_table_2"
然后我得到以下错误消息:
Then i get the following error message as result:
Table 'completely_different_table_2' was not locked with LOCKED TABLES
实际上,MySql告诉我该表未锁定是正确的.但是为什么它会引发错误?有人知道我该如何解决吗?
Indeed, MySql is right to tell me that the table is not locked. But why does it throws an error? Anyone any ideas how I could solve this?
谢谢.
推荐答案
您必须锁定要使用的每个表,直到发布LOCK
为止.您只能给completely_different_table_2
一个READ LOCK
,这允许其他进程在锁定该表时读取该表:
You have to lock every table, that you want to use until the LOCK
is released. You can give completely_different_table_2
only a READ LOCK
, which allows other processes to read this table while it is locked:
LOCK TABLES table_1 WRITE, completely_different_table_2 READ;
PS:MySQL有这样做的理由.如果请求LOCK
,则要冻结数据的一致状态.如果您从LOCK
内部的completely_different_table_2
读取数据,则写入table_1
的数据将在某种程度上取决于此其他表.因此,您不希望任何人在您的LOCK
期间更改此表并也为第二个表请求一个READ LOCK
.如果您写入table_1
的数据不依赖于另一个表,只需在发布LOCK
之前不查询它即可.
PS: MySQL has a reason to do so. If you request a LOCK
, you want to freeze a consistent state of your data. If you read data from completely_different_table_2
inside your LOCK
, your data written to table_1
will in some way depend on this other table. Therefore you don’t want anyone to change this table during your LOCK
and request a READ LOCK
for this second table as well. If your data written to table_1
doesn’t depend on the other table, simply don’t query it until the LOCK
is released.
这篇关于表[表名]未锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!