Answer the question
In order to leave comments, you need to log in
How to correctly organize transactions in MySQL with the return of a result?
Hello. Tell me how to correctly select records from the database in conditions of concurrent access, set the flag "in processing" for them and return the result to whom this flag was set. Now I do this, but I'm not sure if it's correct to rely on the field modification time, because the UPDATE operation can take more than 1 second.
START TRANSACTION;
UPDATE `list` a INNER JOIN
(SELECT id FROM `list` WHERE `processing` = 0 LIMIT 10 FOR UPDATE) b
ON a.id = b.id SET `processing` = -1;
SELECT * FROM `list` WHERE `updated_at`=NOW() AND `processing`=-1;
COMMIT;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question