Reputation: 167
I am writing a scheduler in spring boot, which will do the following action.
When I will run my application as two separate instances, 2 schedulers will run.
Which will make all the above steps repeated in 2 times.
My goal is if the schedule of the 1st instance is reading 2 rows at that time schedule of the 2nd instance to read next 2 rows, instead of the same row.
Please suggest any way to do this using JPA or in Java.
Upvotes: 0
Views: 1243
Reputation: 13858
So let's have a look at your requirements:
Now how to solve them?
So you need some kind of "reservation" on the two rows the process is going to handle.
I'd handle it this way:
FOR UPDATE
, preventing other processes from doing the same. (Yes, that IS blocking others, but only while you're at it)You might want to take extra measures in terms of one process dying after setting lock-marker but before releasing them which could eventually lead to rows never processed - maybe us a timestamp value in lock column and select for lock based on reasonable time delta (process runs every 10 minutes - eveeything older than 30min is probably crashed)
Upvotes: 1