PDA

View Full Version : Locking tables


filburt1
Fri 14th Jan '05, 1:24am
I want to lock one specific table from other threads (or PHP page loads, assuming they're related) from inserting data into it while my thread can still access it. This is the only table I care about; i.e., I don't want any others locked. When I'm done with the INSERTs in my thread, I want to unlock it for all other threads.

LOCK TABLES tablename WRITE doesn't seem to do what I want. When I call it and then query any other table in the same PHP page load, I get database errors like:

Invalid SQL: SELECT extension
FROM attachmenttype
WHERE enabled
mysql error: Table 'attachmenttype' was not locked with LOCK TABLES


What is the proper solution to this? The reason I'm doing it is because I'm making an assumption that a consecutive range of primary IDs exists starting at a certain ID, and if any other threads write to the table during that assumption, it will try to insert to an ID already taken (that I thought wasn't) and cause a collision.

If you know Java, I want to avoid the equivalent of a ConcurrentModificationException and synchronize the table so only my single script session can accces it.

dwh
Mon 17th Jan '05, 7:23am
I'm assuming that you didn't run into any of the pitfalls on this page http://dev.mysql.com/doc/mysql/en/LOCK_TABLES.html

All tables that are locked by the current thread are implicitly unlocked when the thread issues another LOCK TABLES

When you use LOCK TABLES, you must lock all tables that you are going to use in your queries. While the locks obtained with a LOCK TABLES statement are in effect, you cannot access any tables that were not locked by the statement. Also, you cannot use a locked table multiple times in one query - use aliases for that.