简体   繁体   English

关于MyISAM表上的锁定和事务的问题

[英]Question on locking and transactions on MyISAM table

I have a counter field in a myisam table. 我的myisam表中有一个计数器字段。 To update the counter value in a multitasking environment (web server, concurrent queries from PHP) I need to lock the record for update. 要在多任务环境(Web服务器,来自PHP的并发查询)中更新计数器值,我需要锁定记录以进行更新。 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. 据我了解,在MyISAM中,应该锁定整个表,直到事务结束。 And when I initiate concurrent query from PHP, opening script in a browser, it really waits until lock is gone. 当我从PHP启动并发查询时,在浏览器中打开脚本,它实际上要等到锁消失为止。 But If I select all records from a table with mysql.exe - it selects all records even when lock should still be hold. 但是,如果我使用mysql.exe从表中选择所有记录-即使锁定仍应保持,它也会选择所有记录。

So it seems I don't understand something. 所以看来我听不懂。 Please, explain such a behavior. 请解释这种行为。

MyISAM tables don't support transactions - START TRANSACTION and COMMIT do nothing. MyISAM表不支持事务START TRANSACTIONCOMMIT什么也不做。

You can use LOCK TABLES : 您可以使用LOCK TABLES

LOCK TABLES mytable READ;
...
UNLOCK TABLES;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM