com.mongodb.MongoCommandException Java Examples

The following examples show how to use com.mongodb.MongoCommandException. 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: TestDocumentValidation.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Test
public void findAndModify() {
    getMapper().map(DocumentValidation.class);
    getDs().enableDocumentValidation();

    getDs().save(new DocumentValidation("Harold", 100, new Date()));

    Query<DocumentValidation> query = getDs().find(DocumentValidation.class);
    ModifyOptions options = new ModifyOptions()
                                .bypassDocumentValidation(false);
    Modify<DocumentValidation> modify = query.modify(set("number", 5));
    try {
        modify.execute(options);
        fail("Document validation should have complained");
    } catch (MongoCommandException e) {
        // expected
    }

    options.bypassDocumentValidation(true);
    modify.execute(options);

    Assert.assertNotNull(query.filter(eq("number", 5)).iterator(new FindOptions().limit(1))
                              .next());
}
 
Example #2
Source File: MongoTimestampPersistence.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private static Source<Success, NotUsed> repeatableCreateCappedCollectionSource(
        final MongoDatabase database,
        final String collectionName,
        final long cappedCollectionSizeInBytes) {

    final CreateCollectionOptions collectionOptions = new CreateCollectionOptions()
            .capped(true)
            .sizeInBytes(cappedCollectionSizeInBytes)
            .maxDocuments(1);

    return Source.lazily(
            () -> Source.fromPublisher(database.createCollection(collectionName, collectionOptions)))
            .mapMaterializedValue(whatever -> NotUsed.getInstance())
            .withAttributes(Attributes.inputBuffer(1, 1))
            .recoverWithRetries(1, new PFBuilder<Throwable, Source<Success, NotUsed>>()
                    .match(MongoCommandException.class,
                            MongoTimestampPersistence::isCollectionAlreadyExistsError,
                            error -> Source.single(Success.SUCCESS))
                    .build());

}
 
Example #3
Source File: IndexInitializerIT.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void initializeFailsWhenConflictingIndexWithSameNameAlreadyExists() {
    // GIVEN
    final String collectionName = "conflictingIndexWithSameName";
    final List<Index> indices = Collections.singletonList(INDEX_FOO);
    initialize(collectionName, indices);
    assertIndices(collectionName, indices);

    // WHEN / THEN
    final List<Index> newIndices = Arrays.asList(INDEX_BAR,
            INDEX_FOO_CONFLICTING_NAME_OPTION, INDEX_BAZ);
    assertThatExceptionOfType(MongoCommandException.class).isThrownBy(() -> initialize(collectionName, newIndices))
            .satisfies(e -> assertThat(e.getErrorCode()).isEqualTo(MONGO_INDEX_OPTIONS_CONFLICT_ERROR_CODE));
    // verify that bar has been created nevertheless (cause it has been initialized before the error), in
    // contrast to baz
    assertIndices(collectionName, Arrays.asList(INDEX_BAR, INDEX_FOO));
}
 
Example #4
Source File: DatastoreImpl.java    From morphia with Apache License 2.0 6 votes vote down vote up
void enableValidation(final MappedClass mc, final Validation validation) {
    if (validation != null) {
        String collectionName = mc.getCollectionName();
        try {
            getDatabase().runCommand(new Document("collMod", collectionName)
                                         .append("validator", parse(validation.value()))
                                         .append("validationLevel", validation.level().getValue())
                                         .append("validationAction", validation.action().getValue()));
        } catch (MongoCommandException e) {
            if (e.getCode() == 26) {
                getDatabase().createCollection(collectionName,
                    new CreateCollectionOptions()
                        .validationOptions(new ValidationOptions()
                                               .validator(parse(validation.value()))
                                               .validationLevel(validation.level())
                                               .validationAction(validation.action())));
            } else {
                throw e;
            }
        }
    }
}
 
Example #5
Source File: Fixture.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
public static void drop(final MongoNamespace namespace) throws Throwable {
    try {
        getMongoClient().getDatabase(namespace.getDatabaseName())
                .runCommand(new Document("drop", namespace.getCollectionName())).timeout(10, SECONDS).toBlocking().first();
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().contains("ns not found")) {
            throw e;
        }
    }
}
 
Example #6
Source File: ITMongoDBTracing.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void defaultSpanNameIsCommandName_nonCollectionCommand() {
  try {
    database.runCommand(new BsonDocument("dropUser", new BsonString("testUser")));

    // Expected, we are trying to drop a user that doesn't exist
    failBecauseExceptionWasNotThrown(MongoCommandException.class);
  } catch (MongoCommandException e) {
    MutableSpan span = testSpanHandler.takeRemoteSpanWithError(CLIENT, e);

    // "testUser" should not be mistaken as a collection name
    assertThat(span.name()).isEqualTo("dropUser");
  }
}
 
Example #7
Source File: TestTextIndexing.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Test(expected = MongoCommandException.class)
public void shouldNotAllowMultipleTextIndexes() {
    Class<MultipleTextIndexes> clazz = MultipleTextIndexes.class;
    getMapper().map(clazz);
    getMapper().getCollection(clazz).drop();
    getDs().ensureIndexes();
}
 
Example #8
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);
}
 
Example #9
Source File: Fixture.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
public static void drop(final MongoNamespace namespace) throws Throwable {
    try {
        ObservableSubscriber<Document> subscriber = new ObservableSubscriber<Document>();
        getMongoClient().getDatabase(namespace.getDatabaseName())
                .runCommand(new Document("drop", namespace.getCollectionName()))
                .subscribe(subscriber);
        subscriber.await(20, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().contains("ns not found")) {
            throw e;
        }
    }
}
 
Example #10
Source File: Fixture.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
public static void dropDatabase(final String name) throws Throwable {
    if (name == null) {
        return;
    }
    try {
        ObservableSubscriber<Document> subscriber = new ObservableSubscriber<Document>();
        getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1)).subscribe(subscriber);
        subscriber.await(10, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    }
}
 
Example #11
Source File: Fixture.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
public static MongoCollection<Document> initializeCollection(final MongoNamespace namespace) throws Throwable {
    MongoDatabase database = getMongoClient().getDatabase(namespace.getDatabaseName());
    try {
        ObservableSubscriber<Document> subscriber = new ObservableSubscriber<Document>();
        database.runCommand(new Document("drop", namespace.getCollectionName())).subscribe(subscriber);
        subscriber.await(10, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    }
    return database.getCollection(namespace.getCollectionName());
}
 
Example #12
Source File: MongoSourceTask.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
private MongoCursor<BsonDocument> tryCreateCursor(
    final MongoSourceConfig sourceConfig,
    final MongoClient mongoClient,
    final BsonDocument resumeToken) {
  try {
    ChangeStreamIterable<Document> changeStreamIterable =
        getChangeStreamIterable(sourceConfig, mongoClient);
    if (resumeToken != null && supportsStartAfter) {
      LOGGER.info("Resuming the change stream after the previous offset: {}", resumeToken);
      changeStreamIterable.startAfter(resumeToken);
    } else if (resumeToken != null && !invalidatedCursor) {
      LOGGER.info("Resuming the change stream after the previous offset using resumeAfter.");
      changeStreamIterable.resumeAfter(resumeToken);
    } else {
      LOGGER.info("New change stream cursor created without offset.");
    }
    return changeStreamIterable.withDocumentClass(BsonDocument.class).iterator();
  } catch (MongoCommandException e) {
    if (resumeToken != null) {
      if (e.getErrorCode() == 260) {
        invalidatedCursor = true;
        return tryCreateCursor(sourceConfig, mongoClient, null);
      } else if ((e.getErrorCode() == 9 || e.getErrorCode() == 40415)
          && e.getErrorMessage().contains("startAfter")) {
        supportsStartAfter = false;
        return tryCreateCursor(sourceConfig, mongoClient, resumeToken);
      }
    }
    LOGGER.info("Failed to resume change stream: {} {}", e.getErrorMessage(), e.getErrorCode());
    return null;
  }
}
 
Example #13
Source File: Fixture.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
public static void dropDatabase(final String name) throws Throwable {
    if (name == null) {
        return;
    }
    try {
        getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1)).timeout(10, SECONDS).toBlocking().first();
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    }
}
 
Example #14
Source File: Fixture.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
public static MongoCollection<Document> initializeCollection(final MongoNamespace namespace) throws Throwable {
    MongoDatabase database = getMongoClient().getDatabase(namespace.getDatabaseName());
    try {
        database.runCommand(new Document("drop", namespace.getCollectionName())).timeout(10, SECONDS).toBlocking().first();
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    }
    return database.getCollection(namespace.getCollectionName());
}
 
Example #15
Source File: IndexOperations.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static PartialFunction<Throwable, Source<Success, NotUsed>> buildDropIndexRecovery(
        final String indexDescription) {
    return new PFBuilder<Throwable, Source<Success, NotUsed>>()
            .match(MongoCommandException.class, IndexOperations::isIndexNotFound, throwable -> {
                LOGGER.debug("Index <{}> could not be dropped because it does not exist (anymore).",
                        indexDescription);
                return Source.single(Success.SUCCESS);
            })
            .build();
}
 
Example #16
Source File: MongoTimestampPersistence.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isCollectionAlreadyExistsError(final MongoCommandException error) {
    return error.getErrorCode() == COLLECTION_ALREADY_EXISTS_ERROR_CODE;
}
 
Example #17
Source File: IndexOperations.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isIndexNotFound(final MongoCommandException e) {
    return e.getErrorCode() == 27;
}