Mysql lock read a single row for innodb

五迷三道 提交于 2020-01-05 08:18:16

问题


Need help to lock reading(select) row

At second 1 : Terminal 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;

At second 2 : Terminal 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:


Change the second transaction (terminal B) to obtain a share lock:

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

I suggest you review the relevant information in the MySQL Reference Manual here:

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



来源:https://stackoverflow.com/questions/44440152/mysql-lock-read-a-single-row-for-innodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!