I am using the following piece of code to update a token field in a collection –
@Override
public TestObject issueToken(long id, String objectId) {
Query query = new Query();
Criteria criteria = new Criteria(“_id”).is(new ObjectId(objectId));
query.addCriteria(criteria);
Update update = new Update();
update.inc("lastIssuedTokenNo",1);
update.set("lastUpdatedTime", new Date());
FindAndModifyOptions options = new FindAndModifyOptions();
options.upsert(false);
options.returnNew(true);
Testobject tokenIssuedObject = mongoTemplate.findAndModify(
query,update,options,
Testobject.class
);
if(ObjectUtils.isEmpty(tokenIssuedObject)) throw new ObjectRuntimeException(ErrorCodes.OBJECT_NOT_FOUND);
return tokenIssuedObject;
}
But I am facing issue where the updated document returned have the lastIssuedTokenNo same as before the update although the lastUpdatedTime has been updated. This is happening for very few updates and others are working fine
I added an additional field lastUpdateTime and updated its value on every update call, there I was able to find that lastUpdatedTime was getting updated but lastIssuedTokenNo was same as before. Also I checked if there were concurrent update calls being made but there were no concurrent calls so chance of race condition. Also this is happening for only few update calls