M
M
MilkyWay2014-06-20 09:14:38
MySQL
MilkyWay, 2014-06-20 09:14:38

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

1 answer(s)
M
Melkij, 2014-06-20
@MilkyWay

Do select for update first, and for update you will already have a list of id, respectively, update where `id` in
Due to select for update, no one else will intercept the lines.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question