suppose we have a query like this
SELECT * FROM WORKER,
(
SELECT worker_id, time_to_work FROM JOB
WHERE job_id = 7 AND time_to_work < now()
) AS work
WHERE WORKER.worker_id = work>worker_id AND WORKER.occupied = 0
after getting the free workers that can do job number 7 right now, we would want to
1- set returned workers occupied
column to 1
2- set time_to_work
of those jobs (I mean job 7 of returned workers) to 24h into the future.
now the problem is that we might return same worker to two different clients, as two transactions may access the same worker cause we did not use for update
.
but where should I put the for update?
if I put for update
in the inner select, would that solve anything?
if I put for update
in the outer select or in both of them wouldn't that cause deadlocks?