Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import com.mongodb.client.model.ReturnDocument;
import org.bson.Document;

import org.springframework.core.retry.RetryException;
import org.springframework.core.retry.RetryPolicy;
import org.springframework.core.retry.RetryTemplate;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;

Expand All @@ -29,10 +33,14 @@
/**
* @author Mahmoud Ben Hassine
* @author Christoph Strobl
* @author Yanming Zhou
* @since 5.2.0
*/
public class MongoSequenceIncrementer implements DataFieldMaxValueIncrementer {

private final RetryTemplate retryTemplate = new RetryTemplate(
RetryPolicy.builder().includes(DataIntegrityViolationException.class).build());

private final MongoOperations mongoTemplate;

private final String sequenceName;
Expand All @@ -44,11 +52,22 @@ public MongoSequenceIncrementer(MongoOperations mongoTemplate, String sequenceNa

@Override
public long nextLongValue() throws DataAccessException {
return mongoTemplate.execute("BATCH_SEQUENCES",
collection -> collection
try {
return retryTemplate
.execute(() -> mongoTemplate.execute("BATCH_SEQUENCES", collection -> collection
.findOneAndUpdate(new Document("_id", sequenceName), new Document("$inc", new Document("count", 1)),
new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER))
.getLong("count"));
.getLong("count")));
}
catch (RetryException e) {
Throwable cause = e.getCause();
if (cause instanceof DataAccessException ex) {
throw ex;
}
else {
throw new RuntimeException("Failed to retrieve next value of sequence", e);
}
}
}

@Override
Expand Down