I need to perform the batch operation with a Pessimistic lock so that no other can read or write this row during this operation. But I want to increment the page number after every batch so that if the transaction failed or instance died, I will continue from the last page. But with the below code, it is not updating the page until all batches are completed so when restarting the job, it's processing from page=0.
@Transactional
public void process(long id) {
Entity entity = repo.findById(id);
processBatch(entity);
}
void processBatch(Entity entity) {
int totalPages = otherMicroService.getTotalPages(id);
int lastPage = entity.getPage();
for(int page=lastPage; i<totalPages; i++) {
doOperation();
entity.setPage(page);
repo.save(entity);
}
}
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<Entity> findById(int id);
Is there a way to update the page after every batch with PESSIMISTIC_WRITE enabled?
Thanks in Advance.