Example usage for com.mongodb MongoException getCode

List of usage examples for com.mongodb MongoException getCode

Introduction

In this page you can find the example usage for com.mongodb MongoException getCode.

Prototype

public int getCode() 

Source Link

Document

Gets the exception code

Usage

From source file:org.netbeans.modules.mongodb.api.MongoErrorCode.java

License:Open Source License

public static MongoErrorCode of(MongoException ex) {
    return codes.get(ex.getCode());
}

From source file:org.slc.sli.ingestion.model.da.BatchJobMongoDA.java

License:Apache License

@Override
public boolean createFileLatch(String jobId, List<String> fileEntries) {
    try {/*from   ww w  .ja v  a2  s  . c  o m*/
        final BasicDBObject latchObject = new BasicDBObject();
        latchObject.put(BATCHJOBID_FIELDNAME, jobId);
        latchObject.put(FILES, fileEntries);

        RetryMongoCommand retry = new RetryMongoCommand() {

            @Override
            public Object execute() {
                batchJobMongoTemplate.getCollection(FILE_ENTRY_LATCH).insert(latchObject, WriteConcern.SAFE);
                return null;
            }
        };

        retry.executeOperation(numberOfRetries);

    } catch (MongoException me) {
        if (me.getCode() == DUP_KEY_CODE) {
            LOG.debug(me.getMessage());
        }
        return false;
    }
    return true;
}

From source file:org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.java

License:Apache License

void createIndex(IndexDefinitionHolder indexDefinition) {

    try {//w w w . jav a2  s .  c  om

        mongoDbFactory.getDb().getCollection(indexDefinition.getCollection())
                .createIndex(indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions());

    } catch (MongoException ex) {

        if (MongoDbErrorCodes.isDataIntegrityViolationCode(ex.getCode())) {

            DBObject existingIndex = fetchIndexInformation(indexDefinition);
            String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.";

            if (existingIndex != null) {
                message += " Index already defined as '%s'.";
            }

            throw new DataIntegrityViolationException(
                    String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(),
                            indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex),
                    ex);
        }

        RuntimeException exceptionToThrow = mongoDbFactory.getExceptionTranslator()
                .translateExceptionIfPossible(ex);

        throw exceptionToThrow != null ? exceptionToThrow : ex;
    }
}