Alien
Alien

Reputation: 15878

Prevent data persist on retry failure call - Spring Retry

I am using @Retry on a method which is also annotated with @Scheduled and I try to connect with another service using RestTemplate.

@Scheduled
@Retry
public void method(){
//Generate excel by getting data from DB
//Saving the document in DB
//Calling another service using rest template by passing document id
}

The above code works fine but in case of another service down or any exception this complete method gets executed and retry tries to call it again ,so one more document gets inserted in Db.

So the question is can I only run Retry for RestTemplate service call and not for the document creation again?

Upvotes: 1

Views: 456

Answers (1)

Alien
Alien

Reputation: 15878

I read some article in which it was mentioned that we should try to make our retry logic idempotent.

So now checking if the document with the name already exists I do not save document but fetch from the db instead.

This way I solved my issue.

@Scheduled
@Retry
public void method(){
//Generate excel by getting data from Db
//Checking document's availability in db
if(exists){
// Fetching from Db
}else{
//Saving the document in DB
}
//Calling another service using rest template by passing document id
}

Upvotes: 1

Related Questions