Mysql to update

MySQL supports the keyword "for updating". This is how I tested that it works as expected. I opened two browser tabs and executed the following commands in one window.

mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from myxml where id = 2 for update; .... mysql> update myxml set id = 3 where id = 2 limit 1; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> commit; Query OK, 0 rows affected (0.08 sec) 

In another window, I started a transaction and tried to lock the update in the same record.

 mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from myxml where id = 2 for update; Empty set (43.81 sec) 

As you can see from the above example, I could not select the record for 43 seconds when the transaction was processed by another application in window No. 1. As soon as the transaction was completed, I should select the record, but since id 2 was changed to id 3 for the transaction, which was completed first, the record was not returned.

My question is, what are the disadvantages of using the "for update" syntax? If I do not complete the transaction running in window 1, will the record be locked forever?

+4
source share
1 answer

Yes, if transaction # 1 is not committed, these records will be locked forever, unless the connection fails, or innodb decides to cancel the transaction due to a lock being detected.

but since id 2 was changed to id 3 for the transaction that was executed in the first place, the record was not returned.

Isn't that what you want? if not, then you are not using SELECT ... FOR UPDATE correctly. see http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html

+3
source

Source: https://habr.com/ru/post/1333764/


All Articles