Answer the question
In order to leave comments, you need to log in
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
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;
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;
1) make the field "blocked" to update it first, and then everything else.
2) run level serializable
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question