mysql锁为innodb读取单行

时间:2017-06-08 15:42:45

标签: mysql locking innodb

需要帮助锁定阅读(选择)行

在第二个1:A航站楼

START TRANSACTION;
SELECT * FROM tbl_processor WHERE id=1 FOR UPDATE; /*Or using this statement : SELECT * FROM tbl_processor WHERE id=1 LOCK IN READ MODE; */
UPDATE tbl_processor SET content='Updated content';  #Original content was content='Original content'   
SELECT SLEEP(10); #Just to sleep for testing purpose
COMMIT;

在第二个2:B航站楼;

SELECT * FROM tbl_processor WHERE id=1;
#This will return result immediately with content='Original content'
-----> This is my problem... I do not want this statement getting not updated content. It should wait until the Terminal A process completed

1 个答案:

答案 0 :(得分:2)

更改第二个交易(终端B)以获取共享锁:

SELECT * FROM tbl_processor WHERE id=1 LOCK IN SHARE MODE;

我建议您查看MySQL参考手册中的相关信息:

https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html