P
P
Pu4u2015-10-05 16:46:29
MySQL
Pu4u, 2015-10-05 16:46:29

Multithreaded mysql table access?

Several identical scripts work with one table.
Rows are ordered by launch date.
Each script of them selects the first line, changes its start date.
What is the CORRECT way to prevent multiple scripts from trying to work with the same line? For some reason, it was not possible to google ...
Please give a solution, and then send it to read the manuals, you need to fix it today.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alex Safonov, 2015-10-05
@Pu4u

Welcome to the world of locks and transaction isolations
- Select from the table a row with marker 'in_work' == 0;
- Get an exclusive write lock on this row. But not for reading.
- Set the marker 'in_work' = 1 in the line;
- Do the job;
- Set the marker 'in_work' = 0 in the line;

R
Rsa97, 2015-10-05
@Rsa97

UPDATE `table` 
    SET `time` = `time` + INTERVAL 1 DAY
    WHERE @id := `id` AND `time` <= NOW() 
    ORDER BY `time` 
    LIMIT 1;

SELECT * 
    FROM `table` 
    WHERE `id` = @id;

S
shagguboy, 2015-10-05
@shagguboy

1) make the field "blocked" to update it first, and then everything else.
2) run level serializable

M
Max, 2015-10-05
@MaxDukov

well so you correctly think - enter locks.
LOCK TABLE
IMHO LOCK TABLE READ should suffice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question