下面列出了怎么用com.mongodb.DuplicateKeyException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean add(List<JobFeedbackPo> jobFeedbackPos) {
if (CollectionUtils.isEmpty(jobFeedbackPos)) {
return true;
}
for (JobFeedbackPo jobFeedbackPo : jobFeedbackPos) {
String tableName = JobQueueUtils.getFeedbackQueueName(
jobFeedbackPo.getJobRunResult().getJobMeta().getJob().getSubmitNodeGroup());
try {
template.save(tableName, jobFeedbackPo);
} catch (DuplicateKeyException e) {
LOGGER.warn("duplicate key for job feedback po: " + JSON.toJSONString(jobFeedbackPo));
}
}
return true;
}
/**
* When upsert is requested on different versions but same ID there should be duplicate
* key exception thrown by Mongo since there will be an attempt to insert new document (same id
* different version)
* Based on criteria it is a new document, based on primary key ({@code _id}) it exists already.
*/
@Test
public void duplicateKeyUpsertSameKeyDifferentVersions() throws Exception {
ImmutableEntity entity = ImmutableEntity.builder().id("e1").version(0).value("v0").build();
repository.upsert(entity).getUnchecked();
// first upsert successful (document should be with new version)
repository.find(repository.criteria().id(entity.id()).version(0))
.andReplaceFirst(entity.withVersion(1))
.upsert()
.getUnchecked();
try {
// this should fail because here upsert == insert (document e1 with version 0 doesn't exist)
repository.find(repository.criteria().id(entity.id()).version(0))
.andReplaceFirst(entity.withVersion(1))
.upsert()
.getUnchecked();
fail("Should fail with " + DuplicateKeyException.class.getName());
} catch (Exception e) {
MongoAsserts.assertDuplicateKeyException(e);
}
}
private void buildCacheForUser() {
List<User> following = this.userGraphService.getFollowing(user, config.fanout_limit);
this.timelineCache = this.contentService.getContentFor(following, null, config.cache_size_limit);
Collections.reverse(this.timelineCache);
if(this.config.cache_users_posts){
this.postCache = this.contentService.getContentFor(user, null, config.cache_size_limit);
Collections.reverse(this.postCache);
}
try {
this.cacheCollection.save(getCacheDocument());
} catch( DuplicateKeyException e ) {
}
}
private Long initialize() {
try {
ds.insert(MetadataEntity.of(NAME, Long.MIN_VALUE));
return Long.MIN_VALUE;
} catch (DuplicateKeyException e) {
return getValue();
}
}
@Override
public void addNodeGroup(NodeType nodeType, String name) {
try {
NodeGroupPo nodeGroupPo = new NodeGroupPo();
nodeGroupPo.setNodeType(nodeType);
nodeGroupPo.setName(name);
nodeGroupPo.setGmtCreated(SystemClock.now());
template.save(nodeGroupPo);
} catch (DuplicateKeyException e) {
// ignore
}
}
@Override
public boolean add(JobPo jobPo) {
try {
template.save(jobPo);
} catch (DuplicateKeyException e) {
// already exist
throw new DupEntryException(e);
}
return true;
}
@Override
public boolean add(JobPo jobPo) {
try {
template.save(jobPo);
} catch (DuplicateKeyException e) {
// 已经存在
throw new DupEntryException(e);
}
return true;
}
@Override
public boolean add(JobPo jobPo) {
try {
template.save(jobPo);
} catch (DuplicateKeyException e) {
// 已经存在
throw new DupEntryException(e);
}
return true;
}
@Override
public boolean add(JobPo jobPo) {
try {
String tableName = JobQueueUtils.getExecutableQueueName(jobPo.getTaskTrackerNodeGroup());
if (!EXIST_TABLE.contains(tableName)) {
createQueue(jobPo.getTaskTrackerNodeGroup());
}
jobPo.setGmtModified(jobPo.getGmtCreated());
template.save(tableName, jobPo);
} catch (DuplicateKeyException e) {
// 已经存在
throw new DupEntryException(e);
}
return true;
}
@Override
public boolean add(JobPo jobPo) {
try {
template.save(jobPo);
} catch (DuplicateKeyException e) {
// 已经存在
throw new DupEntryException(e);
}
return true;
}
/**
* Ensures current exception has been generated due to a duplicate (primary) key.
* Differentiates between Fongo and Mongo exceptions since the behaviour under these databases
* is different.
*/
public static void assertDuplicateKeyException(Throwable exception) {
Preconditions.checkNotNull(exception, "exception");
// unwrap, if necessary
exception = exception instanceof MongoException ? exception : exception.getCause();
// fongo throws directly DuplicateKeyException
if (exception instanceof DuplicateKeyException) return;
// MongoDB throws custom exception
if (exception instanceof MongoCommandException) {
String codeName = ((MongoCommandException) exception).getResponse().get("codeName").asString().getValue();
int errorCode = ((MongoCommandException) exception).getErrorCode();
check(codeName).is("DuplicateKey");
check(errorCode).is(11000); // code 11000 stands for DuplicateKeyException
// all good here (can return)
return;
}
// for bulk writes as well
if (exception instanceof MongoBulkWriteException) {
List<BulkWriteError> errors = ((MongoBulkWriteException) exception).getWriteErrors();
check(errors).hasSize(1);
check(errors.get(0).getCode()).is(11000);
check(errors.get(0).getMessage()).contains("duplicate key");
return;
}
// if we got here means there is a problem (no duplicate key exception)
fail("Should get duplicate key exception after " + exception);
}