Home  >  Article  >  Backend Development  >  mysql concurrency plus for update is not locked

mysql concurrency plus for update is not locked

WBOY
WBOYOriginal
2016-08-10 09:07:231403browse

I have a long transaction
There is a row-level lock in the middle and back part of it
After testing, it was found that it is not locked
But if you take out this part alone, it can be locked
In my entire long transaction, under what circumstances will it happen? Will it affect my locking?
select Id from product_term where Id=".$v['P_Term_id']." for update

Reply content:

I have a long transaction
There is a row-level lock in the middle and back part of it
After testing, it was found that it is not locked
But if you take out this part alone, it can be locked
In my entire long transaction, under what circumstances will it happen? Will it affect my locking?
select Id from product_term where Id=".$v['P_Term_id']." for update

Settings will be affected.
Actually. . Let me ask a newbie question first. . . Is it Innodb? I encountered a question about Myisam's affairs before. .
Okay. Back to topic.
Exclusive lock will normally lock this query range until the end of the transaction.
Just like you. The record corresponding to the ID row will be locked.
But based on the isolation mode of the transaction. If you have not changed the settings, the default is RR. That is to say, the SELECT you executed before this exclusive lock has the same result. . This will give the illusion that it is not locked. .

If your affairs are like this

<code>BEGIN;
SELECT id FROM product_term where id<100;
UPDATE product_term SET XXX='YYY' WHERE id = 1;
...
SELECT id FROM product_term where id=1 FOR UPDATE;
...
COMMIT;</code>

Like this, due to the default RR level, the first range search already contains id=1, and then the third row obtained under the same transaction will be the same as the first row. In fact, the data in this row has been changed in the second row. If there are two concurrencies, it may appear that the table is not locked~~
It is exclusively recommended to do it at the beginning of the transaction.
I don’t know if you are encountering this problem? If so, you can carefully study the transaction isolation mode, and you will find that there are more pitfalls

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn