D
D
Dmitry Skogorev2015-12-29 16:32:34
MySQL
Dmitry Skogorev, 2015-12-29 16:32:34

How to lock a row in mysql?

Hello
I am writing a project with an asynchronous queue.
I'm thinking of using a mysql table like
Id | worker's answer
Accordingly, the question is how to give each task to only one worker - I think that it is a write lock and I see two options:
Add a column with a state of 0/1, but I'm afraid that two or more workers will have time to grab / update the row
Is it possible to lock the row for writing / reading and carianth is most kosher
Thank you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Night, 2015-12-29
@maxtm

It would be more correct to make the workers accept the task from the master process than they themselves would take the task from the database.

R
Rsa97, 2015-12-29
@Rsa97

UPDATE `table` SET `lock` = 1 WHERE @id := `id` AND `lock` = 0 AND ... LIMIT 1
SELECT * FROM `table` WHERE `id` = @id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question