简体   繁体   English

为多个查询锁定 innoDB 中的行

[英]Locking rows in in innoDB for multiple queries

So I was looking at row locking for innoDB and it seems like SELECT...FOR UPDATE is the go to query for locking.所以我正在查看 innoDB 的行锁定,看起来SELECT...FOR UPDATE是 go 来查询锁定。 In my file I have three queries.在我的文件中,我有三个查询。 Two updates and then an insertion.两次更新,然后一次插入。 Is there a way to make sure that for each connection the rows are locked and then these three queries can run before someone else can change the rows?有没有办法确保对于每个连接,行都被锁定,然后这三个查询可以在其他人更改行之前运行?

Here are the queries:以下是查询:

"UPDATE table SET r=r+2 WHERE r > '$l' AND home='$home'";

"UPDATE table SET l=l+2 WHERE l > '$l' AND home='$home'";

"INSERT INTO table (value, home, l, r, value2)
            VALUES ('$value', '$home', '$l'+1, '$l'+2, value2) ";

To clarify: I want to lock all the rows where home=$home and then free them after the three queries.澄清一下:我想锁定 home=$home 的所有行,然后在三个查询后释放它们。 I want to make sure the three queries are executed before any other connections can have write abilities to those rows.我想确保在任何其他连接可以对这些行具有写入能力之前执行这三个查询。

Use transactions, your changes are not visible to others until you commit them explicitly.使用事务,您的更改在您明确提交之前对其他人不可见。

Further informations: msql docs on transactions更多信息: 关于事务的 msql 文档

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

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