<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:51:45 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-213] Make the Mongo class proxy safe</title>
                <link>https://jira.mongodb.org/browse/JAVA-213</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Currently, when calling new Mongo() the following happen:&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;default hard coded settings are used&lt;/li&gt;
	&lt;li&gt;the driver then tries to establish a connection&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;When using reflection tools like cglib to build a proxy around a class, a common behaviour is to call the default constructor to wrap around it (through a subclass). &lt;/p&gt;

&lt;p&gt;This doesn&apos;t work with the Mongo class (or could in a misleading way if there&apos;s a local mongo server listening on the defaults port, which could change, esp in production).&lt;/p&gt;

&lt;p&gt;IMHO, the best approach here would be to have an explicit call to mongo to open the connection. However, at &quot;worst&quot;, a way to setup the default settings could be enough, esp. if these settings can be done through java. One could also consider not providing a default constructor (since the current one isn&apos;t clean), but it would mean more work when proxying around.&lt;/p&gt;

&lt;p&gt;thanks in advance&lt;/p&gt;</description>
                <environment></environment>
        <key id="13645">JAVA-213</key>
            <summary>Make the Mongo class proxy safe</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="9">Done</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="zedros">ZedroS</reporter>
                        <labels>
                    </labels>
                <created>Mon, 8 Nov 2010 13:03:22 +0000</created>
                <updated>Tue, 5 Dec 2017 15:37:15 +0000</updated>
                            <resolved>Sat, 28 Apr 2012 02:17:03 +0000</resolved>
                                    <version>2.3</version>
                                                    <component>API</component>
                    <component>Codecs</component>
                                        <votes>2</votes>
                                    <watches>6</watches>
                                                                                                                <comments>
                            <comment id="114532" author="jeff.yemin" created="Sat, 28 Apr 2012 02:17:03 +0000"  >&lt;p&gt;Since zero-arg Mongo constructor no longer tries to connect to a local instance, closing this.  Please comment if you disagree.&lt;/p&gt;</comment>
                            <comment id="84752" author="jeff.yemin" created="Thu, 2 Feb 2012 21:19:38 +0000"  >&lt;p&gt;@Kevin, the current version of the driver behaves as you indicate.  Without mongod running, this code:&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;    Mongo mongo = &lt;/span&gt;&lt;span style=&quot;color: #006699; font-weight: bold; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt; Mongo();&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;    DB db = mongo.getDB(&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;&quot;db1&quot;&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;);&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;    DBCollection collection = db.getCollection(&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;&quot;cool&quot;&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;);&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&amp;nbsp;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;    collection.drop();&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;doesn&apos;t throw any exception until the call to drop().&lt;/p&gt;

&lt;p&gt;Can this be closed, or is there more to it?&lt;/p&gt;</comment>
                            <comment id="26561" author="kkkev" created="Wed, 23 Mar 2011 10:01:21 +0000"  >&lt;p&gt;I agree that it should be possible to construct a Mongo object without establishing a connection to the database. In its current form, the Mongo object causes application initialisation to fail in Spring applications that inject Mongo objects into other objects. This makes it very difficult to make Spring applications resistant to Mongo failures, and forces such applications to be started AFTER the Mongo database, thus reducing operational flexibility.&lt;/p&gt;</comment>
                            <comment id="22734" author="remonvv" created="Thu, 27 Jan 2011 08:57:01 +0000"  >&lt;p&gt;The issue is related to the Proxy design pattern I think, not http ones. Am I misunderstanding your comment?&lt;/p&gt;</comment>
                            <comment id="22719" author="korny" created="Thu, 27 Jan 2011 00:37:59 +0000"  >&lt;p&gt;Note that this causes an extremely painful problem on Ubuntu systems (at least) - if you have &quot;useSystemProxies&quot; set in /usr/sun-java-6/net.properties, it sets Java to use the system-wide proxy properties by default.  However, it doesn&apos;t seem to set &quot;nonProxyHosts&quot; properly!&lt;br/&gt;
The net result is a basic Mongo connection to localhost will try to go through the system http proxy, and (in our environment at least) hang indefinitely.&lt;br/&gt;
This isn&apos;t really the Mongo driver&apos;s fault, but it&apos;s very hard to diagnose, and quite counterintuitive that a simple Mongo connect would go looking for a http proxy at all!&lt;/p&gt;
</comment>
                            <comment id="21636" author="remonvv" created="Tue, 21 Dec 2010 13:54:19 +0000"  >&lt;p&gt;I agree that a DB connection should be established when an operation is performed that requires one. For us having the Mongo(..) constructors throwing exceptions means our dependency injection framework (Google Guice) in our case fails to inject Mongo or related classes but cannot (or will not) display the actual cause of the failed injection. This complicates resolving configuration or code issues quite a bit.&lt;/p&gt;</comment>
                            <comment id="21633" author="zedros" created="Tue, 21 Dec 2010 09:49:38 +0000"  >&lt;p&gt;I can&apos;t do &lt;/p&gt;

&lt;p&gt;ProxiedMongo(){ &lt;br/&gt;
  super( new MongoURI( config.getURI() ) ); &lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;For the lazy proxy matter, it simply breaks its intend: the db connection would happen on each lazy proxy creation, not when actually used.&lt;/p&gt;

&lt;p&gt;On the bigger picture  it would mean also I have to inject this ProxiedMongo everywhere, considering that:&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;there&apos;s code I don&apos;t have an hand on (morphia for example)&lt;/li&gt;
	&lt;li&gt;there&apos;s code already written =&amp;gt; would have to be changed&lt;/li&gt;
	&lt;li&gt;one should never forget to use it: if someone somewhere use or inject just mongo it would break it down (using the wrong settings)&lt;/li&gt;
&lt;/ul&gt;
</comment>
                            <comment id="20174" author="eliot" created="Tue, 9 Nov 2010 13:27:39 +0000"  >&lt;p&gt;Why can&apos;t you do&lt;/p&gt;

&lt;p&gt;ProxiedMongo(){&lt;br/&gt;
  super( new MongoURI( config.getURI() ) );&lt;br/&gt;
}&lt;/p&gt;</comment>
                            <comment id="20164" author="zedros" created="Tue, 9 Nov 2010 08:03:54 +0000"  >&lt;p&gt;hi Eliot&lt;/p&gt;

&lt;p&gt;Thanks for your answer&lt;/p&gt;

&lt;p&gt;The issue is linked with proxying, in my case in the context of Wicket but it could happen to anyone using proxy.&lt;/p&gt;

&lt;p&gt;To create a proxy, the approach is usually to extend the proxied class through a subclass. Then a new instance of the subclass will be created, allowing to intercept each call made to the proxied class (and then do whatever needed).&lt;/p&gt;

&lt;p&gt;In the case of the mongo class, having such subclass doesn&apos;t work. Indeed, the subclass must call the parent constructor, and goes at the parameter less constructor. For example like this:&lt;br/&gt;
ProxiedMongo extends Mongo{&lt;/p&gt;

&lt;p&gt;ProxiedMongo(){&lt;br/&gt;
super();&lt;br/&gt;
}&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;calling super here trigger this default settings connection mechanism, which fails when there&apos;s no locally available mongodb instance (in reality, tools like CGLib are used for this subclass creation, which is then done automatically).&lt;/p&gt;

&lt;p&gt;What&apos;s done next after the proxying is deeply use case specific. In my own use case, it does lazy proxing on injected field: first only an &quot;empty&quot; proxy is done. If and when it&apos;s first called, then the real instance of the mongo class will be fetched and used. Used in a web framework, this helps avoiding page getting too big (full of injected members of which most aren&apos;t used on every request). &lt;/p&gt;</comment>
                            <comment id="20159" author="eliot" created="Tue, 9 Nov 2010 07:44:11 +0000"  >&lt;p&gt;I don&apos;t think a config option for overriding localhost makes sense.&lt;/p&gt;

&lt;p&gt;Why can&apos;t you just put the mongo uri string in a config file and instantiate with that?&lt;/p&gt;</comment>
                            <comment id="20158" author="zedros" created="Tue, 9 Nov 2010 07:41:37 +0000"  >&lt;p&gt;what would be your preferred option ? &lt;/p&gt;

&lt;p&gt;For example, if I was to provide a patch providing a config file for the default settings, would it be ok for you ?&lt;/p&gt;

&lt;p&gt;thanks again&lt;/p&gt;</comment>
                            <comment id="20112" author="eliot" created="Mon, 8 Nov 2010 14:58:40 +0000"  >&lt;p&gt;Overall this is the API most people seem most comfortable with.&lt;br/&gt;
Can see over time if that changes&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|hrhcmf:</customfieldvalue>

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