I have 2 tables, one called user, stores users, and one called process that stores the list of active processes updating users.
What I want to do is be able to select 50 out-of-date users, lock them by inserting a record in the process table with the user id range that I am updating, so that if a second process is fired up, it will not start processing the same users.
To sum-up:
- Select in the process table the last processed id
- Select in the user table 50 ids > last processed id
- Insert in the process table a new record with the ids range I am about to process
I want to make sure that if 2 processes (P1 and P2) are fired up at the same time, the 3 steps are executed consecutively for each process. i.e:
- P1 step 1
- P1 step 2
- P1 step 3
- P2 step 1
- P2 step 2
- P2 step 3
and not
- P1 step 1
- P2 step 1
- P1 step 2
- etc.
How should I do that? Transactions or Table locks? Or even something else?
My tables are InnoDB ones.
Thank you!