com.mongodb.DuplicateKeyException Java Examples

The following examples show how to use com.mongodb.DuplicateKeyException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoJobFeedbackQueue.java    From light-task-scheduler with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: SimpleReplacerTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example #3
Source File: ContentCache.java    From socialite with Apache License 2.0 5 votes vote down vote up
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 ) {
        }
    }
 
Example #4
Source File: RevisionManager.java    From alchemy with MIT License 5 votes vote down vote up
private Long initialize() {
    try {
        ds.insert(MetadataEntity.of(NAME, Long.MIN_VALUE));
        return Long.MIN_VALUE;
    } catch (DuplicateKeyException e) {
        return getValue();
    }
}
 
Example #5
Source File: MongoNodeGroupStore.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
@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
    }
}
 
Example #6
Source File: MongoExecutingJobQueue.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean add(JobPo jobPo) {
    try {
        template.save(jobPo);
    } catch (DuplicateKeyException e) {
        // already exist
        throw new DupEntryException(e);
    }
    return true;
}
 
Example #7
Source File: MongoCronJobQueue.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean add(JobPo jobPo) {
    try {
        template.save(jobPo);
    } catch (DuplicateKeyException e) {
        // 已经存在
        throw new DupEntryException(e);
    }
    return true;
}
 
Example #8
Source File: MongoRepeatJobQueue.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean add(JobPo jobPo) {
    try {
        template.save(jobPo);
    } catch (DuplicateKeyException e) {
        // 已经存在
        throw new DupEntryException(e);
    }
    return true;
}
 
Example #9
Source File: MongoExecutableJobQueue.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #10
Source File: MongoSuspendJobQueue.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean add(JobPo jobPo) {
    try {
        template.save(jobPo);
    } catch (DuplicateKeyException e) {
        // 已经存在
        throw new DupEntryException(e);
    }
    return true;
}
 
Example #11
Source File: MongoAsserts.java    From immutables with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}