Java insertion error handling: error return vs. thrown exception, querying for inserted documents

817 views
Skip to first unread message

Michael Siegel

unread,
Sep 25, 2012, 7:10:48 PM9/25/12
to mongod...@googlegroups.com
Hi,

I'm a little bit confused as to when an exception is thrown and when an error code is returned, using DBCollection.insert(List<DBObject>).  I'm using v2.7.3 of the Java Mongo driver.  The documentation (<http://api.mongodb.org/java/2.7.3/com/mongodb/DBCollection.html#insert(java.util.List)>) doesn't specify the conditions under which certain codes are returned or MongosteenException is thrown.  The example here, <https://github.com/mongodb/mongo-snippets/blob/master/java/Test.java>, seems to suggest that MongosteenException is thrown when any insertion error occurs.  But, then why would a return code be provided?  How should they be used together?

I'm specifically interested in handling recoverable errors, such as primary failover, so that insertions can be tried again if they failed the first time, using the approach of querying for the _ids of the documents that were supposed to have been inserted to determine which ones need further insertion attempts (<https://groups.google.com/d/topic/mongodb-user/NX3WQ89lK-Q/discussion>).  But, I don't want to keep trying if the error is not recoverable, if it's possible to distinguish between the two cases.

Thanks.

Guillaume Forget

unread,
Sep 26, 2012, 12:38:40 PM9/26/12
to mongod...@googlegroups.com
Hello Michael,

It all depends on the WriteConcern you are using when running the insert. WriteConcern has some predefined instances. The javadoc (http://api.mongodb.org/java/2.7.3/com/mongodb/WriteConcern.html) of these constants specify the behavior on error. 

Gui

Michael Siegel

unread,
Sep 26, 2012, 1:38:44 PM9/26/12
to mongod...@googlegroups.com
Right, I understand the various write concerns that one can request.  What I don't quite get is the role of the method's return value vs the role the possible exception it could throw.  insert both returns a WriteResult and can throw a MongoException, according to the documentation.

I'm guessing there are three possible end cases for an insertion:
- the insertion succeeds in reference to the requested write concern
- the insertion is known to have failed before the level of success specified by the write concern has been reached
- there is a timeout before a response to the getLastError request is received (if a finite timeout is requested)

Obviously, no exception is thrown in the success case.  However, is there ever an error return value?  Or, is an exception always thrown in the case of an error?  Is there a way to programmaticallly determine the nature of the error based on what is returned or thrown so that a decision can be made as to whether or not to try a failed insertion again?  Also, do you mean the behavior as to whether an exception is thrown or an error code is returned is dependent on the write concern (and not just by setting the success criteria)?

Bryan

unread,
Sep 26, 2012, 8:41:04 PM9/26/12
to mongod...@googlegroups.com
Write failures always throw a MongoException, (or subclass of MongoException). When using a safe(r) write concern, the driver will send a getLastError query to the DB. The driver then parses the JSON response and throws an exception if either the "ok" field != 1, or the "err" field != null.

Michael Siegel

unread,
Sep 27, 2012, 1:19:47 PM9/27/12
to mongod...@googlegroups.com
Thanks!  This makes sense.

Bryan

unread,
Sep 28, 2012, 4:13:39 PM9/28/12
to mongod...@googlegroups.com
I need to make an addendum:  For version 2.7.3 of the driver, some errors from the server may not be thrown as a MongoException. I'd recommend using the latest version of the driver, but  You can programmatically get the server error thusly:

        WriteResult writeResult = null;
        WriteConcern concern = new WriteConcern();
        DBObject document = new BasicDBObject ("detent", concern.toString()) ;
        
        try {
            writeResult = demo.collection.insert(document, concern);
        } catch ( MongoException  e ) {
            e.printStackTrace();
            System.out.println("Caught MongoException cause: "+e.getMessage());
        }   finally
            
            CommandResult cmdResult = writeResult.getLastError();
            if( !cmdResult.ok()) 
                System.out.println("error : "+cmdResult.getErrorMessage());
Reply all
Reply to author
Forward
0 new messages