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#inser...)>) 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.
On Tuesday, September 25, 2012 4:10:48 PM UTC-7, Michael Siegel wrote:
> 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#inser...)>) > 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.
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)?
On Wednesday, September 26, 2012 12:38:40 PM UTC-4, Guillaume Forget wrote:
> 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.
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.
On Wednesday, September 26, 2012 10:38:44 AM UTC-7, Michael Siegel wrote:
> 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)?
> On Wednesday, September 26, 2012 12:38:40 PM UTC-4, Guillaume Forget wrote:
>> 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.
On Wednesday, September 26, 2012 8:41:04 PM UTC-4, Bryan wrote:
> 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.
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()) ;
On Thursday, September 27, 2012 10:19:47 AM UTC-7, Michael Siegel wrote:
> Thanks! This makes sense.
> On Wednesday, September 26, 2012 8:41:04 PM UTC-4, Bryan wrote:
>> 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.