Search in sources :

Example 26 with MongoException

use of com.mongodb.MongoException in project mongo-java-driver by mongodb.

the class ProtocolHelper method throwWriteException.

@SuppressWarnings("deprecation")
private static void throwWriteException(final BsonDocument result, final ServerAddress serverAddress) {
    MongoException specialException = createSpecialException(result, serverAddress, "err");
    if (specialException != null) {
        throw specialException;
    }
    int code = WriteConcernException.extractErrorCode(result);
    if (ErrorCategory.fromErrorCode(code) == ErrorCategory.DUPLICATE_KEY) {
        throw new DuplicateKeyException(result, serverAddress, createWriteResult(result));
    } else {
        throw new WriteConcernException(result, serverAddress, createWriteResult(result));
    }
}
Also used : MongoException(com.mongodb.MongoException) WriteConcernException(com.mongodb.WriteConcernException) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Example 27 with MongoException

use of com.mongodb.MongoException in project mongo-java-driver by mongodb.

the class InternalStreamConnection method open.

@Override
public void open() {
    isTrue("Open already called", stream == null);
    stream = streamFactory.create(serverId.getAddress());
    try {
        stream.open();
        description = connectionInitializer.initialize(this);
        opened.set(true);
        connectionListener.connectionOpened(new ConnectionOpenedEvent(getId()));
        LOGGER.info(format("Opened connection [%s] to %s", getId(), serverId.getAddress()));
    } catch (Throwable t) {
        close();
        if (t instanceof MongoException) {
            throw (MongoException) t;
        } else {
            throw new MongoException(t.toString(), t);
        }
    }
}
Also used : ConnectionOpenedEvent(com.mongodb.event.ConnectionOpenedEvent) MongoException(com.mongodb.MongoException)

Example 28 with MongoException

use of com.mongodb.MongoException in project mongo-java-driver by mongodb.

the class AsyncQueryBatchCursor method handleGetMoreQueryResult.

private void handleGetMoreQueryResult(final AsyncConnection connection, final SingleResultCallback<List<T>> callback, final QueryResult<T> result) {
    if (isClosed()) {
        connection.release();
        connectionSource.release();
        callback.onResult(null, new MongoException("The cursor was closed before next() completed."));
        return;
    }
    cursor.getAndSet(result.getCursor());
    if (result.getResults().isEmpty() && result.getCursor() != null) {
        getMore(connection, result.getCursor(), callback);
    } else {
        count += result.getResults().size();
        if (limitReached()) {
            killCursor(connection);
            connection.release();
        } else {
            connection.release();
            connectionSource.release();
        }
        if (result.getResults().isEmpty()) {
            callback.onResult(null, null);
        } else {
            callback.onResult(result.getResults(), null);
        }
    }
}
Also used : MongoException(com.mongodb.MongoException)

Example 29 with MongoException

use of com.mongodb.MongoException in project spring-boot by spring-projects.

the class MongoHealthIndicatorTests method mongoIsDown.

@Test
public void mongoIsDown() throws Exception {
    MongoTemplate mongoTemplate = mock(MongoTemplate.class);
    given(mongoTemplate.executeCommand("{ buildInfo: 1 }")).willThrow(new MongoException("Connection failed"));
    MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate);
    Health health = healthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat((String) health.getDetails().get("error")).contains("Connection failed");
    verify(mongoTemplate).executeCommand("{ buildInfo: 1 }");
}
Also used : MongoException(com.mongodb.MongoException) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Test(org.junit.Test)

Example 30 with MongoException

use of com.mongodb.MongoException in project graylog2-server by Graylog2.

the class MongoConnectionImpl method connect.

/**
     * Connect the instance.
     */
@Override
public synchronized Mongo connect() {
    if (m == null) {
        final String dbName = mongoClientURI.getDatabase();
        if (isNullOrEmpty(dbName)) {
            LOG.error("The MongoDB database name must not be null or empty (mongodb_uri was: {})", mongoClientURI);
            throw new RuntimeException("MongoDB database name is missing.");
        }
        m = new MongoClient(mongoClientURI);
        db = m.getDB(dbName);
        db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        mongoDatabase = m.getDatabase(dbName).withWriteConcern(WriteConcern.ACKNOWLEDGED);
    }
    try {
        db.command("{ ping: 1 }");
    } catch (MongoCommandException e) {
        if (e.getCode() == 18) {
            throw new MongoException("Couldn't connect to MongoDB. Please check the authentication credentials.", e);
        } else {
            throw new MongoException("Couldn't connect to MongoDB: " + e.getMessage(), e);
        }
    }
    final Version mongoVersion = getMongoVersion(m.getDB("admin"));
    if (mongoVersion != null && mongoVersion.lessThan(MINIMUM_MONGODB_VERSION)) {
        LOG.warn("You're running MongoDB {} but Graylog requires at least MongoDB {}. Please upgrade.", mongoVersion, MINIMUM_MONGODB_VERSION);
    }
    return m;
}
Also used : MongoClient(com.mongodb.MongoClient) MongoException(com.mongodb.MongoException) MongoCommandException(com.mongodb.MongoCommandException) Version(com.github.zafarkhaja.semver.Version)

Aggregations

MongoException (com.mongodb.MongoException)42 BasicDBObject (com.mongodb.BasicDBObject)22 DBObject (com.mongodb.DBObject)21 DBCollection (com.mongodb.DBCollection)16 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 DBCursor (com.mongodb.DBCursor)8 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)4 IOException (java.io.IOException)4 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 JSONException (org.json.JSONException)4 Stopwatch (com.google.common.base.Stopwatch)3 BasicDBList (com.mongodb.BasicDBList)3 DuplicateKeyException (com.mongodb.DuplicateKeyException)3 MongoClient (com.mongodb.MongoClient)3 BulkWriteOperation (com.mongodb.BulkWriteOperation)2 CommandResult (com.mongodb.CommandResult)2 MongoClientURI (com.mongodb.MongoClientURI)2