0

I implemented the Scheduler in spring boot and the application is deployed to 3 pods. this scheduler is connected to 2 Mongo DB collections. 1st collection has id, name, indexId, state. in the implementation, find all the documents that has state "PENDING" and update them to "INPROGRESS". and then, collect data related to each document's indexId from external API and save those data in another collection one indexId per time inside a loop. And update the related indexed state to "COMPLETED" in the first Collection.

This scheduler runs in multiple pods. Hence it will cause concurrency Error. To resolve that I used Optimistic Lock with Spring Retry.

Following is my DTO class.

@Data
public class Customer {

    @Id
    private String id;
    private String name;
    private String indexId;
    
    @Version
    private Long version;
}

Following is my RetryConfig class.

@Configuration
public class RetryConfig extends RetryConfiguration {

    @Override
    public int getOrder() {
        return -1;
    }

}

Following is my Service class.

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    OrderRepo orderRepo;

    @Retryable(include = {UncategorizedMongoDbException.class, MongoTransactionException.class})
    @Transactional
    @Override
    public void getStudentDetails() {
        List<Customer> customer = repo.findUpdateStudentBystatus("PENDING", "INPROGRESS");
        customer.forEach(customer1 -> {
            ....
            studentRepos.save(studentInfo);
            repo.findUpdateStudentBystatus("INPROGRESS", "COMPLETED");
        });
        
    }

}

And also I am using mongoTemplate.updateMulti(query, update, Customer.class) function for update multiple documents.

This is working without @Transactional annotation. But when I add the @Transactional annotation I get the following Error. UncotegorizedMongoDbException: Command failed with error 112 (write Conflict): WriteConflict error: this operation conflicts with another operation. Please retry your operation or multi-document transaction.

Is my implementation correct? How to solve this error ?

Devmina
  • 45
  • 9

0 Answers0