com.mongodb.ErrorCategory Java Examples

The following examples show how to use com.mongodb.ErrorCategory. 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: MongoEntityStorage.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void create(final Entity entity) throws EntityStorageException {
    requireNonNull(entity);

    try {
        final boolean hasDuplicate = detectDuplicates(entity);

        if (!hasDuplicate) {
            mongo.getDatabase(ryaInstanceName)
                .getCollection(COLLECTION_NAME)
                .insertOne( ENTITY_CONVERTER.toDocument(entity) );
        } else {
            throw new EntityNearDuplicateException("Duplicate data found and will not be inserted for Entity with Subject: "  + entity);
        }
    } catch(final MongoException e) {
        final ErrorCategory category = ErrorCategory.fromErrorCode( e.getCode() );
        if(category == ErrorCategory.DUPLICATE_KEY) {
            throw new EntityAlreadyExistsException("Failed to create Entity with Subject '" + entity.getSubject().getData() + "'.", e);
        }
        throw new EntityStorageException("Failed to create Entity with Subject '" + entity.getSubject().getData() + "'.", e);
    }
}
 
Example #2
Source File: MongoEventStorage.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void create(final Event event) throws EventStorageException {
    requireNonNull(event);

    try {
        mongo.getDatabase(ryaInstanceName)
            .getCollection(COLLECTION_NAME)
            .insertOne(EVENT_CONVERTER.toDocument(event));
    } catch(final MongoException e) {
        final ErrorCategory category = ErrorCategory.fromErrorCode( e.getCode() );
        if(category == ErrorCategory.DUPLICATE_KEY) {
            throw new EventAlreadyExistsException("Failed to create Event with Subject '" + event.getSubject().getData() + "'.", e);
        }
        throw new EventStorageException("Failed to create Event with Subject '" + event.getSubject().getData() + "'.", e);
    }
}
 
Example #3
Source File: LockDao.java    From mongobee with Apache License 2.0 6 votes vote down vote up
public boolean acquireLock(MongoDatabase db) {

    Document insertObj = new Document(KEY_PROP_NAME, LOCK_ENTRY_KEY_VAL).append("status", "LOCK_HELD");

    // acquire lock by attempting to insert the same value in the collection - if it already exists (i.e. lock held)
    // there will be an exception
    try {
      db.getCollection(lockCollectionName).insertOne(insertObj);
    } catch (MongoWriteException ex) {
      if (ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
        logger.warn("Duplicate key exception while acquireLock. Probably the lock has been already acquired.");
      }
      return false;
    }
    return true;
  }
 
Example #4
Source File: MongoDbDeviceRegistryUtils.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Checks if the given error is caused due to duplicate keys.
 *
 * @param error The error to check.
 * @return {@code true} if the given error is caused by duplicate keys.
 * @throws NullPointerException if the error is {@code null}.
 */
public static boolean isDuplicateKeyError(final Throwable error) {
    Objects.requireNonNull(error);

    if (error instanceof MongoException) {
        final MongoException mongoException = (MongoException) error;
        return ErrorCategory.fromErrorCode(mongoException.getCode()) == ErrorCategory.DUPLICATE_KEY;
    }
    return false;
}