Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
If you try to call drupal_write_record('some_table', ...) after acquiring a lock on that table using db_lock_table('some_table'), you will get tons of errors having to do with locks on other tables. Here's an excerpt:
user warning: Table 'cache' was not locked with LOCK TABLES query: SELECT data, created, headers, expire, serialized FROM cache WHERE cid = 'schema' in /home/threexk/public_html/sandbox_d6_cvs/includes/cache.inc on line 26.
Test code that causes error:
db_lock_table('schematest');
$record = new StdClass();
$record->field1 = 'abc';
$record->field2 = 123;
drupal_write_record('schematest', $record);
db_unlock_tables();
Attached is (1) the module used to generate the test case, and (2) a copy/paste of all errors.
MySQL: 5.0.67
PHP memory limit: 5.2.6
In case it's relevant, my MySQL max_allowed_packet=16M and my PHP memory limit is 512MB. I tried upping the max_allowed_packet based on discussions of other issues, but that had no effect.
Comment | File | Size | Author |
---|---|---|---|
errors.txt | 79.63 KB | threexk | |
schematest.tgz | 475 bytes | threexk |
Comments
Comment #1
threexk CreditAttribution: threexk commentedHere are the grants for the database user (as given by "show grants"):
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON `sandbox_d6_cvs`.* TO 'sandbox'@'localhost'
Comment #2
mkalkbrennerI ran into the same issue. See #675952: "Tables not locked" error when creating rule
Here's the part of the striped code:
The Problem is that drupal_write_record() calls drupal_get_schema() which uses caching. The cache table isn't locked.
But drupal_get_schema() additionally caches the schema in an internal static variable. That's the reason why drupal_write_record() in combination with db_lock_table() doesn't always fail.
By knowing this you can do that workaround: