<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:51:56 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[JAVA-292] writeResult.getLastError().throwOnError() does not throw MongoException.DuplicateKey on duplicate key error</title>
                <link>https://jira.mongodb.org/browse/JAVA-292</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;When an entity is inserted twice into a collection the returned WriteResult should throw a MongoException (DuplicateKey) from&lt;br/&gt;
  writeResult.getLastError().throwOnError();&lt;/p&gt;

&lt;p&gt;This is the case when the dbobject is inserted via collection.insert( dbo, WriteConcern.SAFE ), but not, when the dbobject is inserted via collection.insert( dbo ).&lt;br/&gt;
After  writeResult.getLastError().throwOnError() the wrtieResult.err property is populated, but no exception has been thrown - which I had expected.&lt;/p&gt;

&lt;p&gt;I tested this with the java driver from github master (commit e3c837a7 from 2011-03-04).&lt;/p&gt;


&lt;p&gt;The following test case shows the described behaviour:&lt;/p&gt;

&lt;p&gt;    @Test&lt;br/&gt;
    public void testDetectDuplicateKeyErrorWithDefaultWriteConcern() throws UnknownHostException, MongoException, InterruptedException {&lt;br/&gt;
        final Mongo mongo = new Mongo( &quot;localhost&quot; );&lt;br/&gt;
        final DBCollection collection = mongo.getDB( &quot;foo&quot; ).getCollection( &quot;testcollection&quot; );&lt;br/&gt;
        final BasicDBObject dbo = new BasicDBObject();&lt;br/&gt;
        collection.insert( dbo );&lt;br/&gt;
        Thread.sleep( 2000 );&lt;br/&gt;
        final WriteResult writeResult = collection.insert( dbo ); // when invoked with WriteConcern.SAFE the error is detected / exception thrown&lt;br/&gt;
        try {&lt;br/&gt;
            writeResult.getLastError().throwOnError();&lt;br/&gt;
            // now the writeResult.err property is populated, but now exception is thrown&lt;br/&gt;
            Assert.fail( &quot;mongodb should complain about a duplicate key error index (MongoException.DuplicateKey), s.th. like the following:\n&quot; +&lt;br/&gt;
                     &quot;\&quot;err\&quot; : \&quot;E11000 duplicate key error index: foo.testcollection.$&lt;em&gt;id&lt;/em&gt;  dup key: { : ObjectId(&apos;4d75fea23a75528813a7eedf&apos;) }\&quot;&quot; );&lt;br/&gt;
        } catch( final Exception e ) &lt;/p&gt;
{
            // expected
        }
&lt;p&gt;    }&lt;/p&gt;</description>
                <environment></environment>
        <key id="15031">JAVA-292</key>
            <summary>writeResult.getLastError().throwOnError() does not throw MongoException.DuplicateKey on duplicate key error</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="scotthernandez">Scott Hernandez</assignee>
                                    <reporter username="martin.grotzke">Martin Grotzke</reporter>
                        <labels>
                    </labels>
                <created>Tue, 8 Mar 2011 10:13:45 +0000</created>
                <updated>Thu, 7 Apr 2011 05:05:15 +0000</updated>
                            <resolved>Tue, 5 Apr 2011 02:28:39 +0000</resolved>
                                    <version>2.5</version>
                                    <fixVersion>2.5.3</fixVersion>
                                                        <votes>0</votes>
                                    <watches>1</watches>
                                                                                                                <comments>
                            <comment id="27772" author="auto" created="Mon, 4 Apr 2011 22:54:26 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;login&apos;: u&apos;scotthernandez&apos;, u&apos;name&apos;: u&apos;Scott Hernandez&apos;, u&apos;email&apos;: u&apos;scotthernandez@gmail.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-292&quot; title=&quot;writeResult.getLastError().throwOnError() does not throw MongoException.DuplicateKey on duplicate key error&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-292&quot;&gt;&lt;del&gt;JAVA-292&lt;/del&gt;&lt;/a&gt; fixes WriteResult.getLastError().throwOnError()&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/66118a6a8dd646d44cf9fae9a0797f78354956d6&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/66118a6a8dd646d44cf9fae9a0797f78354956d6&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="25588" author="martin.grotzke" created="Wed, 9 Mar 2011 08:38:28 +0000"  >&lt;p&gt;Sorry for the confusing issue report!&lt;/p&gt;

&lt;p&gt;Regarding the documentation of the default WriteConcern: it would be great to have this mentioned in DBCollection.insert(DBObject) with a link to the clarification of (default) WriteConcern. Perhaps it&apos;s good to mention explicitely that &quot;even&quot; a duplicate key error is ignored with default WriteConcern and that insert(dbobject, WriteConcern.SAFE) should be used to recognize such errors?&lt;/p&gt;

&lt;p&gt;Btw: is it &quot;better&quot; (in terms of performance) to use insert(dbobject, WriteConcern.SAFE) or to use insert(dbobject) + lastError.throwOnError? Does getLastError cause an additional server lookup?&lt;/p&gt;</comment>
                            <comment id="25514" author="scotthernandez" created="Tue, 8 Mar 2011 16:23:56 +0000"  >&lt;p&gt;Sorry, I misunderstood that throwOnError wasn&apos;t throwing... That is something I can look into.&lt;/p&gt;

&lt;p&gt;As for the documentation, I do believe that default WriteConcern is discussed; I&apos;ll take a look specifically. Do you have a suggestion where you expected but didn&apos;t see it? &lt;/p&gt;</comment>
                            <comment id="25513" author="martin.grotzke" created="Tue, 8 Mar 2011 16:04:14 +0000"  >&lt;p&gt;Ok, I just followed the path of the default WriteConcern in DBCollection.insert and AFAICS the Mongo.concern (WriteConcern.NORMAL) is used when nothing else is specified for a DB or DBCollection.&lt;br/&gt;
Is this documented somewhere? IMHO it would be good for users to get this information not too far from DBCollection.insert. For DBCollection.save it&apos;s documented that the default WriteConcern is used. For insert this information is IMHO more relevant, perhaps it would also be good to mention that duplicate keys are not detected with the default WriteConcern being NORMAL.&lt;/p&gt;

&lt;p&gt;Another issue IMHO is that getLastError.throwOnError is expected not to throw an exception in the case above. Is there some documentation that I should read to get the mind set to follow this?&lt;br/&gt;
throwOnError tells me the following: &quot;throws an exception containing the cmd name, in case the command failed&quot; - and therefore I&apos;d expect that this throws an exception in the case the command failed. Perhaps this documentation should contain more conditions regarding the WriteConcern used before?&lt;/p&gt;</comment>
                            <comment id="25508" author="scotthernandez" created="Tue, 8 Mar 2011 15:33:25 +0000"  >&lt;p&gt;If you don&apos;t use a WriteConcern.SAFE/STRICT or greater then the exception is not thrown since the error was not detected during the write operation; that is expected behavior. When you later call to check the error, then the error is returned but no exception is thrown &amp;#8211; that is also expected behavior.&lt;/p&gt;</comment>
                            <comment id="25501" author="martin.grotzke" created="Tue, 8 Mar 2011 10:23:06 +0000"  >&lt;p&gt;I just realized that the collection.insert should already throw the MongoException.DuplicateKey. So the title is wrong, and the collection.insert in the test would have to be pulled into the try/catch.&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrhcen:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>14810</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>