views:

340

answers:

1

Hello, I have a counter field in a myisam table. To update the counter value in a multitasking environment (web server, concurrent queries from PHP) I need to lock the record for update. So I do it like this:

START TRANSACTION; 
SELECT Counter FROM mytable ... FOR UPDATE; 
UPDATE Counter value or INSERT INTO mytable; 
// let's make sleep for 20 seconds here to make transaction longer 
COMMIT;

As I understand, in MyISAM the whole table should be locked until transaction ends. And when I initiate concurrent query from PHP, opening script in a browser, it really waits until lock is gone. But If I select all records from a table with mysql.exe - it selects all records even when lock should still be hold.

So it seems I don't understand something. Please, explain such a behavior.

+3  A: 

MyISAM tables don't support transactions - START TRANSACTION and COMMIT do nothing.

You can use LOCK TABLES:

LOCK TABLES mytable READ;
...
UNLOCK TABLES;
Greg
Thank you for your reply. It's true. It seems it was my webserver who was blocked (Apache) and couldn't make 2 queries at the same time. Very strange.
nightcoder