Mysql concurrency plus forupdate is not locked

Source: Internet
Author: User
I have a long transaction in which a row-level lock is tested and found that it is not locked, but this part is taken out separately, and can be locked in my entire long transaction, under what circumstances will my locks be affected? SelectIdfromproduct_termwhereId & quot;. $ v [& #039; P_Term_id & #039;]. & quot; forup... I have a long transaction
A row-level lock in the middle and back of the lock
The test showed that the lock was not found.
However, you can lock this part separately.
In my entire long transaction, under what circumstances will my locks be affected?
Select Id from product_term where Id = ". $ v ['p _ Term_id ']." for update

Reply content:

I have a long transaction
A row-level lock in the middle and back of the lock
The test showed that the lock was not found.
However, you can lock this part separately.
In my entire long transaction, under what circumstances will my locks be affected?
Select Id from product_term where Id = ". $ v ['p _ Term_id ']." for update

The setting will be affected.
Actually .. First ask a question about Tom... Is it Innodb? I encountered a problem with Myisam asking about transactions ..
Okay. Return to the question.
The exclusive lock locks the query range until the transaction ends.
Just like you. The record of the corresponding ID row is locked.
However, it depends on the transaction isolation mode. If you have not changed the settings, the default setting is RR. That is to say, the SELECT statement executed before the exclusive lock has the same result .. This produces the illusion of being unlocked ..

Assume that your transaction is like this.

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;

Like this, because of the default RR level, the first range search already contains id = 1, and then the row obtained by the third in the same transaction is the same as the first line. In fact, the second row has changed the data of this row. If two concurrent threads exist, the table may not appear locked ~~
We recommend that you do this at the beginning of the transaction.
I wonder if you have encountered this problem? If yes, you can carefully study the transaction isolation mode and you will find more pitfalls.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.