<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:52:30 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-528] Question about WriteConcern and Java driver concurrency using &quot;update if current&quot;</title>
                <link>https://jira.mongodb.org/browse/JAVA-528</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;I am attempting to implement an &quot;update if current&quot; strategy with the MongoDB Java driver and had a couple questions. Our system reads and writes concurrently to Mongo and it is important that we avoid dirty reads. Here are the questions I had while implementing it since it&apos;s not that trivial to test at this point:&lt;/p&gt;

&lt;p&gt;1) Do I need to use the requestStart and requestDone methods for the Mongo connection for this strategy? If I was not guaranteed to be using the same connection, the threads could be reading or attempting the update on either the master or slave which means there is a window of opportunity during which an update could still be accepted since the changes made by the thread on another connection have not been propagated yet. Here&apos;s an example:&lt;/p&gt;

&lt;p&gt;Thread_A reads message X from connection to Mongo_M&lt;br/&gt;
Thread_B reads message X from connection to Mongo_S&lt;br/&gt;
Thread_A updates some fields, creating message Y. Thread_A sends update details on Mongo_S.&lt;br/&gt;
Thread_B updates some fields, creating message Z. Thread_B sends update details on Mongo_M.&lt;br/&gt;
Mongo_S propagates data (message Y) to Mongo_M (contains message Z).&lt;/p&gt;

&lt;p&gt;2) If in my update in the &quot;update if current&quot; strategy (I am using the WriteConcern.SAFE option), is a Java exception thrown if an error condition occurs during the update?&lt;/p&gt;

&lt;p&gt;Any assistance would be appreciated, thanks.&lt;/p&gt;</description>
                <environment></environment>
        <key id="31960">JAVA-528</key>
            <summary>Question about WriteConcern and Java driver concurrency using &quot;update if current&quot;</summary>
                <type id="3" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14718&amp;avatarType=issuetype">Task</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="-1">Unassigned</assignee>
                                    <reporter username="ccho@gilt.com">Chris Cho</reporter>
                        <labels>
                    </labels>
                <created>Thu, 1 Mar 2012 19:55:42 +0000</created>
                <updated>Wed, 11 Sep 2019 19:13:08 +0000</updated>
                            <resolved>Thu, 1 Mar 2012 23:39:17 +0000</resolved>
                                                                    <component>Connection Management</component>
                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="94411" author="jeff.yemin" created="Thu, 1 Mar 2012 23:39:17 +0000"  >&lt;p&gt;OK, closing this then.&lt;/p&gt;</comment>
                            <comment id="94366" author="ccho@gilt.com" created="Thu, 1 Mar 2012 22:12:01 +0000"  >&lt;p&gt;That makes sense since the writes are routed to the master, thanks.&lt;/p&gt;</comment>
                            <comment id="94339" author="jeff.yemin" created="Thu, 1 Mar 2012 20:56:23 +0000"  >&lt;p&gt;For this particular use case, it might be better to use &lt;a href=&quot;http://www.mongodb.org/display/DOCS/findAndModify+Command&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://www.mongodb.org/display/DOCS/findAndModify+Command&lt;/a&gt;.&lt;/p&gt;
</comment>
                            <comment id="94334" author="ccho@gilt.com" created="Thu, 1 Mar 2012 20:49:16 +0000"  >&lt;p&gt;Hi Jeff,&lt;/p&gt;

&lt;p&gt;Thanks for clearing up that writes are routed to the master. I am following the &quot;update if current&quot; strategy on the Atomic Operations page to prevent threads from competing over the same document. Here are some details of my implementation, can you take a look and see if my strategy is correct?&lt;/p&gt;

&lt;p&gt;1) Retrieve one document with findOne() with criteria of &lt;/p&gt;
{&quot;status&quot;: &quot;unprocessed&quot;}
&lt;p&gt;.&lt;br/&gt;
2) Extract _id and status field and package into a query.&lt;br/&gt;
3) Create an update query using atomic &quot;$set&quot; and &lt;/p&gt;
{&quot;status&quot; :&quot;processing&quot;}
&lt;p&gt;.&lt;br/&gt;
4) Run an update on the collection using the new query and update query with WriteConcern.SAFE level and upsert=false.&lt;br/&gt;
5) Catch MongoException which I&apos;m assuming is thrown when the update cannot go through since there are no documents that match.&lt;/p&gt;
</comment>
                            <comment id="94312" author="jeff.yemin" created="Thu, 1 Mar 2012 20:25:16 +0000"  >&lt;p&gt;Is &quot;Mongo_M&quot; the master, and &quot;Mongo_S&quot; the slave in your example?  If so, it can&apos;t happen as you describe, as all writes are routed to the master.&lt;/p&gt;

&lt;p&gt;But even given that all writes go the master, it is possible for one thread to overwrite another threads changes to the same document if they don&apos;t coordinate the activity in some way.  There are a number of features that can help you, depending on the nature of those updates. To get started you can look at:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.mongodb.org/display/DOCS/Atomic+Operations&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://www.mongodb.org/display/DOCS/Atomic+Operations&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;Also, as implied on that page, you can never have a &quot;dirty read&quot; in MongoDB in the way that you may be thinking from RDBMS.&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|hrh4fr:</customfieldvalue>

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