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
Message from discussion Java insertion error handling: error return vs. thrown exception, querying for inserted documents

Received: by 10.68.212.168 with SMTP id nl8mr691264pbc.5.1348706475412;
        Wed, 26 Sep 2012 17:41:15 -0700 (PDT)
X-BeenThere: mongodb-user@googlegroups.com
Received: by 10.68.237.161 with SMTP id vd1ls7624743pbc.3.gmail; Wed, 26 Sep
 2012 17:41:04 -0700 (PDT)
Received: by 10.68.197.70 with SMTP id is6mr903076pbc.14.1348706464908;
        Wed, 26 Sep 2012 17:41:04 -0700 (PDT)
Date: Wed, 26 Sep 2012 17:41:04 -0700 (PDT)
From: Bryan <bryan.rein...@10gen.com>
To: mongodb-user@googlegroups.com
Message-Id: <735b541f-3dea-42f9-9f3b-d54663a8b2f5@googlegroups.com>
In-Reply-To: <a98d56b1-2fe3-4806-8b70-e22dc444c826@googlegroups.com>
References: <43756274-9a68-4081-aa2f-5f3a6c3c7687@googlegroups.com>
 <f805551c-827e-4a2f-9978-10b213468353@googlegroups.com>
 <a98d56b1-2fe3-4806-8b70-e22dc444c826@googlegroups.com>
Subject: Re: Java insertion error handling: error return vs. thrown
 exception, querying for inserted documents
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_343_31221510.1348706464284"

------=_Part_343_31221510.1348706464284
Content-Type: multipart/alternative; 
	boundary="----=_Part_344_15715662.1348706464284"

------=_Part_344_15715662.1348706464284
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

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. 
>>
>> Gui
>>
>>
------=_Part_344_15715662.1348706464284
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<span style=3D"color: rgb(0, 0, 0); font-family: Arial, FreeSans, Helvetica=
, sans-serif; line-height: 17px; background-color: rgb(224, 240, 255); ">Wr=
ite failures always throw a MongoException, (or subclass of MongoException)=
. When using a safe(r) write concern, the driver will send a getLastError q=
uery to the DB. The driver then parses the JSON response and throws an exce=
ption if either the "ok" field !=3D 1, or the "err" field !=3D null.</span>=
<br><br>On Wednesday, September 26, 2012 10:38:44 AM UTC-7, Michael Siegel =
wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8=
ex;border-left: 1px #ccc solid;padding-left: 1ex;">Right, I understand the =
various write concerns that one can request.&nbsp; What I don't quite get i=
s the role of the method's return value vs the role the possible exception =
it could throw.&nbsp; insert both returns a WriteResult and can throw a Mon=
goException, according to the documentation.<br><br>I'm guessing there are =
three possible end cases for an insertion:<br>- the insertion succeeds in r=
eference to the requested write concern<br>- the insertion is known to have=
 failed before the level of success specified by the write concern has been=
 reached<br>- there is a timeout before a response to the getLastError requ=
est is received (if a finite timeout is requested)<br><br>Obviously, no exc=
eption is thrown in the success case.&nbsp; However, is there ever an error=
 return value?&nbsp; Or, is an exception always thrown in the case of an er=
ror?&nbsp; 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?&nbsp; Also, do you mean=
 the behavior as to whether an exception is thrown or an error code is retu=
rned is dependent on the write concern (and not just by setting the success=
 criteria)?<br><br><br>On Wednesday, September 26, 2012 12:38:40 PM UTC-4, =
Guillaume Forget wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;=
margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Michae=
l,<div><br></div><div>It all depends on the WriteConcern you are using when=
 running the insert. WriteConcern has some predefined instances. The javado=
c (<a href=3D"http://api.mongodb.org/java/2.7.3/com/mongodb/WriteConcern.ht=
ml" target=3D"_blank">http://api.mongodb.org/java/<wbr>2.7.3/com/mongodb/<w=
br>WriteConcern.html</a>) of these constants specify the behavior on error.=
&nbsp;</div><div><br></div><div>Gui<br><br></div></blockquote></blockquote>
------=_Part_344_15715662.1348706464284--

------=_Part_343_31221510.1348706464284--