Search in sources :

Example 1 with MongoQueryException

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

the class FindOperation method exceptionTransformingCallback.

private static <T> SingleResultCallback<T> exceptionTransformingCallback(final SingleResultCallback<T> callback) {
    return new SingleResultCallback<T>() {

        @Override
        public void onResult(final T result, final Throwable t) {
            if (t != null) {
                if (t instanceof MongoCommandException) {
                    MongoCommandException commandException = (MongoCommandException) t;
                    callback.onResult(result, new MongoQueryException(commandException.getServerAddress(), commandException.getErrorCode(), commandException.getErrorMessage()));
                } else {
                    callback.onResult(result, t);
                }
            } else {
                callback.onResult(result, null);
            }
        }
    };
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) SingleResultCallback(com.mongodb.async.SingleResultCallback) MongoQueryException(com.mongodb.MongoQueryException)

Example 2 with MongoQueryException

use of com.mongodb.MongoQueryException in project jackrabbit-oak by apache.

the class MongoStatus method isMajorityReadConcernEnabled.

/**
     * Check if the majority read concern is enabled and can be used for queries.
     *
     * @return true if the majority read concern is enabled
     */
public boolean isMajorityReadConcernEnabled() {
    if (majorityReadConcernEnabled == null) {
        // Mongo API doesn't seem to provide an option to check whether the
        // majority read concern has been enabled, so we have to try to use
        // it and optionally catch the exception.
        DBCollection emptyCollection = db.getCollection("emptyCollection-" + System.currentTimeMillis());
        DBCursor cursor = emptyCollection.find(new BasicDBObject(), new DBCollectionFindOptions().readConcern(ReadConcern.MAJORITY));
        try {
            cursor.hasNext();
            majorityReadConcernEnabled = true;
        } catch (MongoQueryException | IllegalArgumentException e) {
            majorityReadConcernEnabled = false;
        } finally {
            cursor.close();
        }
    }
    return majorityReadConcernEnabled;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) DBCollectionFindOptions(com.mongodb.client.model.DBCollectionFindOptions) MongoQueryException(com.mongodb.MongoQueryException)

Aggregations

MongoQueryException (com.mongodb.MongoQueryException)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBCollection (com.mongodb.DBCollection)1 DBCursor (com.mongodb.DBCursor)1 MongoCommandException (com.mongodb.MongoCommandException)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 DBCollectionFindOptions (com.mongodb.client.model.DBCollectionFindOptions)1