[英]Table [tablename] is not locked
I am writing a MySQL query that locks a table: 我正在编写一个锁定表的MySQL查询:
"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
Indeed, MySql is right to tell me that the table is not locked. 的确,MySql告诉我表没有锁定是正确的。 But why does it throws an error?
但为什么会抛出错误? Anyone any ideas how I could solve this?
任何想法我怎么能解决这个问题?
Thanks in advance. 提前致谢。
You have to lock every table , that you want to use until the LOCK
is released. 您必须锁定要使用的每个表 ,直到
LOCK
被释放。 You can give completely_different_table_2
only a READ LOCK
, which allows other processes to read this table while it is locked: 你可以给
completely_different_table_2
只有一个READ LOCK
,它允许同时锁定其他进程读取该表:
LOCK TABLES table_1 WRITE, completely_different_table_2 READ;
PS: MySQL has a reason to do so. PS:MySQL有理由这样做。 If you request a
LOCK
, you want to freeze a consistent state of your data. 如果您请求
LOCK
,则需要冻结数据的一致状态。 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. 如果从读取数据
completely_different_table_2
你里面LOCK
,您的数据写入到table_1
会以某种方式依赖于这个其他表。 Therefore you don't want anyone to change this table during your LOCK
and request a READ LOCK
for this second table as well. 因此,您不希望任何人在您的
LOCK
期间更改此表,并为此第二个表请求READ LOCK
。 If your data written to table_1
doesn't depend on the other table, simply don't query it until the LOCK
is released. 如果写入
table_1
的数据不依赖于另一个表,则只需在LOCK
释放LOCK
查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.