<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:57: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-2544] Messy way to determine if a Client connection is successful</title>
                <link>https://jira.mongodb.org/browse/JAVA-2544</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Currently, when you try to connect to a remote MongoDB server, the connection will appear to be successful even though the remote server is down. The only indication is from the log messages but the Java API continues as normal.&lt;/p&gt;

&lt;p&gt;To make a connection and then check the connection is successful then Java programs need do something similar to the following...&lt;/p&gt;

&lt;p&gt;MongoClient mongoClient = new MongoClient();&lt;br/&gt;
mongoClient.getAddress();&lt;/p&gt;

&lt;p&gt;I&apos;d expect the first line to throw an exception if a connection to any server (in a server list say) failed but it doesn&apos;t so the second line is required in order to do it. Very annoying.&lt;/p&gt;

&lt;p&gt;I propose that either an immediate MongoException is thrown by the MongoClient or you introduce a method where the connection can be tested (e.g. an isClosed or whatever method on the MongoClient class). Both would be good though. One for immediate connection problems and the other for subsequent checks by the application.&lt;/p&gt;</description>
                <environment></environment>
        <key id="393269">JAVA-2544</key>
            <summary>Messy way to determine if a Client connection is successful</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</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="13202">Works as Designed</resolution>
                                        <assignee username="ross@mongodb.com">Ross Lawley</assignee>
                                    <reporter username="aharris">Andrew Harris</reporter>
                        <labels>
                    </labels>
                <created>Tue, 13 Jun 2017 08:27:48 +0000</created>
                <updated>Fri, 27 Oct 2023 13:21:12 +0000</updated>
                            <resolved>Tue, 27 Jun 2017 14:26:39 +0000</resolved>
                                                                    <component>Cluster Management</component>
                                        <votes>0</votes>
                                    <watches>7</watches>
                                                                                                                <comments>
                            <comment id="1607596" author="ross@10gen.com" created="Tue, 27 Jun 2017 14:26:39 +0000"  >&lt;p&gt;Marking this as &quot;works as designed&quot; for the reasons discussed. &lt;/p&gt;

&lt;p&gt;As mentioned the Listener inferfaces can also be used to determine the state of the cluster.&lt;/p&gt;

&lt;p&gt;All the best,&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                            <comment id="1606774" author="jeff.yemin" created="Mon, 26 Jun 2017 18:14:48 +0000"  >&lt;p&gt;There&apos;s also &lt;tt&gt;ClusterListener&lt;/tt&gt;, which is a bit higher level that &lt;tt&gt;ServerMonitorListener&lt;/tt&gt;.  The reference documentation has a simple example of its use: &lt;a href=&quot;http://mongodb.github.io/mongo-java-driver/3.4/driver/reference/monitoring/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://mongodb.github.io/mongo-java-driver/3.4/driver/reference/monitoring/&lt;/a&gt;.&lt;/p&gt;
</comment>
                            <comment id="1606763" author="ross@10gen.com" created="Mon, 26 Jun 2017 18:03:36 +0000"  >&lt;p&gt;&lt;tt&gt;ServerMonitorListener&lt;/tt&gt; is available in 3.3.  We are also improving the usability for the &lt;tt&gt;ConnectionListener&lt;/tt&gt; and &lt;tt&gt;ConnectionPoolListener&lt;/tt&gt; in 3.5  - see &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-805&quot; title=&quot;Make connection pool and connection state observable&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-805&quot;&gt;&lt;del&gt;JAVA-805&lt;/del&gt;&lt;/a&gt; for more information.&lt;/p&gt;</comment>
                            <comment id="1606759" author="aharris" created="Mon, 26 Jun 2017 18:00:04 +0000"  >&lt;p&gt;That listener idea is interesting. Is that only possible in 3.5+?&lt;/p&gt;

&lt;p&gt;Regards,&lt;/p&gt;

&lt;p&gt;Andy&lt;/p&gt;

</comment>
                            <comment id="1606596" author="ross@10gen.com" created="Mon, 26 Jun 2017 16:07:02 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=aharris&quot; class=&quot;user-hover&quot; rel=&quot;aharris&quot;&gt;aharris&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Apologies if I was unclear in my response. When I mentioned its a complicated issue, this is because without knowing the exact read preference or write concern of any given operation, it can be impossible to determine if the &quot;connection&quot; is successful or not.  For example when connecting to a replicaset - if for some reason there is no primary (due to an election) then writes will fail, where as secondary reads would succeed. In that scenario, the definition of a successful connection can change depending on the users action.  &lt;/p&gt;

&lt;p&gt;An alternative example, is when connecting to a Sharded Cluster, if a user provides multiple MongoS to connect to then the driver is resilient. If any MongoS is down for any reason then operations can continue without impacting the system. In fact, it is this design that allows for rolling server upgrades without downtime for users.&lt;/p&gt;

&lt;p&gt;One alternative, would be to use the Listener API to detect server state, The &lt;a href=&quot;http://mongodb.github.io/mongo-java-driver/3.5/javadoc/com/mongodb/event/ServerMonitorListener.html&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;ServerMonitorListener&lt;/a&gt; which can be configured in the &lt;tt&gt;MongoClientOptions&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;I hope helps,&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                            <comment id="1606554" author="aharris" created="Mon, 26 Jun 2017 15:42:05 +0000"  >&lt;p&gt;That&apos;s no different to how I do it now but I don&apos;t accept your argument&lt;br/&gt;
that it is a complicated issue. Surely the driver must know when it is&lt;br/&gt;
connected to something (a socket connection is established) and so that&lt;br/&gt;
state can be read through the java driver api?&lt;/p&gt;



</comment>
                            <comment id="1606244" author="ross@10gen.com" created="Mon, 26 Jun 2017 11:04:56 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=aharris&quot; class=&quot;user-hover&quot; rel=&quot;aharris&quot;&gt;aharris&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Many thanks for your ticket.  When you create a &lt;tt&gt;MongoClient&lt;/tt&gt; in the Java driver, a connection pool is created in the background. The process follows the &lt;a href=&quot;https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;Server Discovery and Monitoring Specifcation&lt;/a&gt;. As you might expect in a distributed system, discovery and monitoring of services can be a non trivial process.  As distributed systems are complex, they are designed to be fault tolerant, a great example of that is MongoDB&apos;s ReplicaSet system that provides fault tolerance and automatic failover.&lt;/p&gt;

&lt;p&gt;The notion of a successful connection is further complicated in MongoDB. As MongoDB is a replicated fault tolerant distributed system users are given the flexibility about where reads come from and where writes occur. This is specified in the &lt;a href=&quot;https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;Server Selection specification&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;All in all, this makes the question about whether or not any given &lt;tt&gt;MongoClient&lt;/tt&gt; is successful non-trivial.  It can depend on many factors and how the client is intended to be used.  Perhaps the best way to ensure that the connection is valid at a given point in time, for a given read / write preference is to issue a command to the server. I have seen users issue a command such as &lt;tt&gt;MongoClient#listDatabases&lt;/tt&gt; or &lt;tt&gt;MongoDatabase#listCollections&lt;/tt&gt;, to achieve this aim.&lt;/p&gt;

&lt;p&gt;I hope that helps,&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                            <comment id="1604659" author="mark.agarunov" created="Thu, 22 Jun 2017 20:53:27 +0000"  >&lt;p&gt;Hello &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=aharris&quot; class=&quot;user-hover&quot; rel=&quot;aharris&quot;&gt;aharris&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Thank you for the report. I&apos;ve set the fixVersion to &quot;Needs Triage&quot; for this new feature to be scheduled against our currently planned work. Updates will be posted on this ticket as they happen.&lt;/p&gt;

&lt;p&gt;Thanks,&lt;br/&gt;
Mark&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|ht13nz:</customfieldvalue>

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