Why gives mysql the error ER_TABLE_NOT_LOCKED while I just locked the table?

风格不统一 提交于 2019-12-23 23:53:29

问题


I have some code that does a LOCK TABLE on a table in my database.

After the lock, I do a SELECT from this same table. My mysql API interface gives the following error:

mysqlsel/db server: Table 'Mytable' was not locked with LOCK TABLES

According some googling, this relays to the error code: ER_TABLE_NOT_LOCKED

Why would this code generate this error? I would not expect it to give an error if I do not lock it at all, and I certainly do not expect an error if I lock it either.


回答1:


I think I solved my own question by 'RTM',

A session that requires locks must acquire all the locks that it needs in a single LOCK TABLES statement. While the locks thus obtained are held, the session can access only the locked tables. For example, in the following sequence of statements, an error occurs for the attempt to access t2 because it was not locked in the LOCK TABLES statement:

I locked multiple tables in separate lock statements,

So if I do:

LOCK TABLES Mytable WRITE
LOCK TABLES Mytable2 WRITE

... a subsequent SELECT FROM Mytable probably triggers this error...



来源:https://stackoverflow.com/questions/6473120/why-gives-mysql-the-error-er-table-not-locked-while-i-just-locked-the-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!