How to implement default Retry for UncategorizedMongoDbException, for any transactions?

Viewed 22

I want to apply a default Retry for any MongoDB Transactions when encountered UncategorizedMongoDbException Write Conflict.

I have created a custom annotation for retry mechanism.

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Retryable(
        value = {UncategorizedMongoDbException.class},
        exceptionExpression = "#{message.contains('WriteConflict error')}",
        maxAttempts = 3,
        backoff = @Backoff(delay = 1000)
)
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class, timeout = 120)
public @interface MongoRetryTransactional {
}

But I have to apply this annotation for places manually.

I want to know if there is any better way to do it ?

0 Answers
Related