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.
You have to lock every table, that you want to use until the
LOCKis released. You can givecompletely_different_table_2only aREAD LOCK, which allows other processes to read this table while it is locked: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 fromcompletely_different_table_2inside yourLOCK, your data written totable_1will in some way depend on this other table. Therefore you don’t want anyone to change this table during yourLOCKand request aREAD LOCKfor this second table as well. If your data written totable_1doesn’t depend on the other table, simply don’t query it until theLOCKis released.