T
T
TransparentFox2017-01-26 20:23:12
PHP
TransparentFox, 2017-01-26 20:23:12

How to make sure that a row from the database is up to date before an important UPDATE query?

For example, there is a table with state and owner_id fields.
One http request handler gets a row from the table, checks that state = "await", and if so, changes the owner_id.
In this case, the user can send an http request, the handler of which will change the state, if the owner_id is equal to the id of the current user.
But if we imagine such a situation that http requests were sent at the same time. Each handler, having received the original row from the table, will make sure that all conditions are met and send an UPDATE query to the database.
Those. as a result, it will turn out that one of the handlers will receive an "obsolete" (at the time the UPDATE request was sent) row from the database, and will send an invalid UPDATE request, which would not have been sent when checking the current row...
The only solution I see is to add a WHERE clause to the UPDATE request of each handler, which checks that the key fields have not changed since the row was received. Or a more universal way - a condition that checks that the updated_at field has not changed since the row was received.
Key questions:
Are there other solutions to this problem?
Maybe it has a name so you can google it?)
Maybe I missed something important?)
update:
Answering my last question: yes, I missed it, I didn't read about how locks work.
https://habrahabr.ru/post/46542/
https://habrahabr.ru/post/238513/
Thanks for the answers)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2017-01-26
@TransparentFox

InnoDB
Transactions

A
Abdula Magomedov, 2017-01-26
@Avarskiy

When you make an update request, immediately change the await status to something else like 'not_await'.
What is the problem, I can't figure it out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question