I have a timer based job
@Component
public class Worker {
@Scheduled(fixedDelay = 100)
public void processEnvironmentActions() {
Job job = pickJob();
}
public Job pickJob() {
Job job = jobRepository.findFirstByStatus(Status.NOT_PROCESSED);
job.setStatus(Status.PROCESSING);
jobRepository.save(job);
return job;
}
}
Now, in the most situations this should give me correct result. But what will happen if there are two instances of microservice executing this piece of code at the same time?
How do I make sure that even if there are multiple instances of service, the repository should always give one job to only one instance and not other instances.
EDIT:
I think people are getting confused/concentrated over @Transactional
so removed it. The question remains the same.