Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Java insertion error handling: error return vs. thrown exception, querying for inserted documents
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Michael Siegel  
View profile  
 More options Sep 25 2012, 7:10 pm
From: Michael Siegel <masiegel...@gmail.com>
Date: Tue, 25 Sep 2012 16:10:48 -0700 (PDT)
Local: Tues, Sep 25 2012 7:10 pm
Subject: Java insertion error handling: error return vs. thrown exception, querying for inserted documents

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.

Thanks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guillaume Forget  
View profile  
 More options Sep 26 2012, 12:38 pm
From: Guillaume Forget <gfor...@gmail.com>
Date: Wed, 26 Sep 2012 09:38:40 -0700 (PDT)
Local: Wed, Sep 26 2012 12:38 pm
Subject: Re: Java insertion error handling: error return vs. thrown exception, querying for inserted documents

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Siegel  
View profile  
 More options Sep 26 2012, 1:38 pm
From: Michael Siegel <masiegel...@gmail.com>
Date: Wed, 26 Sep 2012 10:38:44 -0700 (PDT)
Local: Wed, Sep 26 2012 1:38 pm
Subject: Re: Java insertion error handling: error return vs. thrown exception, querying for inserted documents

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)?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bryan  
View profile  
 More options Sep 26 2012, 8:41 pm
From: Bryan <bryan.rein...@10gen.com>
Date: Wed, 26 Sep 2012 17:41:04 -0700 (PDT)
Local: Wed, Sep 26 2012 8:41 pm
Subject: Re: Java insertion error handling: error return vs. thrown exception, querying for inserted documents

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Siegel  
View profile  
 More options Sep 27 2012, 1:19 pm
From: Michael Siegel <masiegel...@gmail.com>
Date: Thu, 27 Sep 2012 10:19:47 -0700 (PDT)
Local: Thurs, Sep 27 2012 1:19 pm
Subject: Re: Java insertion error handling: error return vs. thrown exception, querying for inserted documents

Thanks!  This makes sense.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bryan  
View profile  
 More options Sep 28 2012, 4:13 pm
From: Bryan <bryan.rein...@10gen.com>
Date: Fri, 28 Sep 2012 13:13:39 -0700 (PDT)
Local: Fri, Sep 28 2012 4:13 pm
Subject: Re: Java insertion error handling: error return vs. thrown exception, querying for inserted documents

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());

        }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »