Reputation: 895
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
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?
Thanks in advance.
Upvotes: 2
Views: 1180
Reputation: 3679
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 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.
Upvotes: 2