3

I would like to handle failure of insert to collection (using Java) in order to be sure that my insert was successful. In case that insert fails, I want to perform some fall-back action.

Assuming following code in Java and latest mongo driver (version 2.11.3):

BasicDBObject doc = new BasicDBObject("name", "MongoDB");
WriteResult result = coll.insert(WriteConcern.SAFE, doc);

I am confused by the fact that the insert returns WriteResult and it could throw MongoException. What am I supposed to do in order to safely detect any possible failure of insert? Please, provide code example. And if you can clarify when insert throws exception and when it just returns some error write result. I tried to search in java driver API docs at http://api.mongodb.org/java/2.11.3/ for it; howerever, this infromation is missing there.

WriteResult result;
try {
    result = coll.insert(WriteConcern.SAFE, doc);
}
catch(MongoException ex){
    logger.warn("Insert failed.", ex);
    throw ex;
}

//Shall I check result here for additional errors?  

If I should check, what type of error I am able to detect by checking insert result?

1 Answer 1

3

You need to take a look at "WriteConcern", it has the all behaviors you need.

You can use it per one write like this:

coll.insert(dbObj, WriteConcern.SAFE);

If you use WriteConcern.SAFE your operation will wait for an acknowledgement from the primary server, so if no exception is raised then you're ok.

Or you can set default behaviour for all write operations when you are creating MongoClient:

MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.writeConcern(WriteConcern.JOURNAL_SAFE);
MongoClient mongoClient = new MongoClient(
    new ServerAddress("localhost"), builder.build());

[Based on Colin Morelli's comment] If you don't use a WriteConcern that raises exceptions, you can use the WriteResult.getLastError() to determine if it was successful or not. Similarly, if you use WriteConcern.SAFE, and the write succeeds, WriteResult will have useful information on it such as the number of records that were written.

Here you can read about WriteConcern in general.

Sign up to request clarification or add additional context in comments.

4 Comments

I have updated my original question a little bit. Now, I am supposing that WriteConcern.SAFE is used while inserting. Shall I check WriteResult in this case or catching MongoException is enough?
Yeah, it would be enough if you handle it in a right for the specific business requirements way.
@EddieJamsession The return value and exception both exist for different reasons. If you don't do a safe WriteConcern, the method would never throw an exception, and you can use the WriteResult.getLastError() to determine if it was successful or not. Similarly, if you use WriteConcern.SAFE, and the write succeeds, WriteResult will have useful information on it such as the number of records that were written. That said, if you're using WriteConcern.SAFE and an exception is not thrown, the data was inserted.
WriteResult returned from insert(doc, WriteConcern.SAFE) does not give you the number of doc's inserted in my testing: WriteResult: { "serverUsed" : "127.0.0.1:27017" , "ok" : 1 , "n" : 0} .. and the document was inserted. I am using 2.6.5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.