<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:51:57 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-293] Cursor is not closed after limit is fully fetched from server</title>
                <link>https://jira.mongodb.org/browse/JAVA-293</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;We&apos;re trying to load all docs from a collection in batches of 100 (to avoid locking the DB for a long period of time).  Our objects are large and this eventually causes the MongoDB server to run out of memory.&lt;/p&gt;

&lt;p&gt;I&apos;ve run a test locally to recreate the problem and it seems that cursors on the server are not being closed.  When running the code below the number of open cursors on the server gradually increases.&lt;/p&gt;

&lt;p&gt;                int batchSize = 100;&lt;br/&gt;
		int i = 0;&lt;br/&gt;
		while(true) &lt;/p&gt;
{
			Iterable&amp;lt;DBObject&amp;gt; cursor = collection.find(new BasicDBObject(ID, new BasicDBObject(GREATER_THAN, i))).sort(new BasicDBObject(ID, 1)).limit(batchSize);
			List&amp;lt;DBObject&amp;gt; dbos = ImmutableList.copyOf(cursor);
			i += batchSize;
			Thread.sleep(1000);
		}

&lt;p&gt;The output of db.serverStatus() after about 1 min:&lt;/p&gt;

&lt;p&gt;&quot;cursros&quot; : &lt;/p&gt;
{
		&quot;totalOpen&quot; : 51,
		&quot;clientCursors_size&quot; : 51,
		&quot;timedOut&quot; : 0
	}
&lt;p&gt;,&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;It appears that using a negative limit fixes this, but there appears to be a 4MB upper limit of the number of docs returned.  Why does the server keep the cursor open when all the results have been read?&lt;/p&gt;
</description>
                <environment></environment>
        <key id="15032">JAVA-293</key>
            <summary>Cursor is not closed after limit is fully fetched from server</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="antoine">Antoine Girbal</assignee>
                                    <reporter username="johna">John Ayres</reporter>
                        <labels>
                    </labels>
                <created>Tue, 8 Mar 2011 11:57:26 +0000</created>
                <updated>Thu, 17 Mar 2011 19:13:55 +0000</updated>
                            <resolved>Wed, 9 Mar 2011 14:58:42 +0000</resolved>
                                    <version>2.4</version>
                                    <fixVersion>2.5</fixVersion>
                                    <component>API</component>
                                        <votes>0</votes>
                                    <watches>3</watches>
                                                                                                                <comments>
                            <comment id="25593" author="auto" created="Wed, 9 Mar 2011 15:08:08 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;login&apos;: u&apos;agirbal&apos;, u&apos;name&apos;: u&apos;agirbal&apos;, u&apos;email&apos;: u&apos;antoine@10gen.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-293&quot; title=&quot;Cursor is not closed after limit is fully fetched from server&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-293&quot;&gt;&lt;del&gt;JAVA-293&lt;/del&gt;&lt;/a&gt;: added tests&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/911c19b8f2baac8c68a7f5e867385596d5857525&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/911c19b8f2baac8c68a7f5e867385596d5857525&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="25585" author="antoine" created="Wed, 9 Mar 2011 06:24:14 +0000"  >&lt;p&gt;This turned out to be more of a refactoring than hoped, to clean up code while fixing issue.&lt;br/&gt;
Solution:&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;positive batchsize tells server the ideal size of a batch returned&lt;/li&gt;
	&lt;li&gt;negative batchsize tells server to return 1 batch of maximum that size, or whatever fits in a 4MB batch, and close the cursor&lt;/li&gt;
	&lt;li&gt;positive limit does limit the total number of documents to return on cursor. Once limit is reached, cursor should be called automatically by driver.&lt;/li&gt;
	&lt;li&gt;negative limit is only supported for legacy reason. It sets the batchSize to that negative value.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;this mimic the underlying variable of the protocol, so will be easier to support.&lt;/p&gt;

&lt;p&gt;Note that for explain(), the value of limit has to be made negative (guess that&apos;s how server needs it).&lt;br/&gt;
It means that in this case, limit&lt;img class=&quot;emoticon&quot; src=&quot;https://jira.mongodb.org/images/icons/emoticons/thumbs_down.png&quot; height=&quot;16&quot; width=&quot;16&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt; and batchSize(-n) is equivalent, which should be fine since there is no actual batch transfer for explain().&lt;/p&gt;</comment>
                            <comment id="25584" author="auto" created="Wed, 9 Mar 2011 06:21:11 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;login&apos;: u&apos;agirbal&apos;, u&apos;name&apos;: u&apos;agirbal&apos;, u&apos;email&apos;: u&apos;antoine@10gen.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-293&quot; title=&quot;Cursor is not closed after limit is fully fetched from server&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-293&quot;&gt;&lt;del&gt;JAVA-293&lt;/del&gt;&lt;/a&gt;: Cursor is not closed after limit is fully fetched from server.&lt;br/&gt;
This turned out to be more of a refactoring than hoped, to clean up code while fixing issue.&lt;br/&gt;
Solution:&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;positive batchsize tells server the ideal size of a batch returned&lt;/li&gt;
	&lt;li&gt;negative batchsize tells server to return 1 batch of maximum that size, or whatever fits in a 4MB batch, and close the cursor&lt;/li&gt;
	&lt;li&gt;positive limit does limit the total number of documents to return on cursor. Once limit is reached, cursor should be called automatically by driver.&lt;/li&gt;
	&lt;li&gt;negative limit is only supported for legacy reason. It sets the batchSize to that negative value.&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/978ad37eb3483d673c1612310251614c7dee804c&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/978ad37eb3483d673c1612310251614c7dee804c&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</comment>
                            <comment id="25520" author="antoine" created="Tue, 8 Mar 2011 17:23:29 +0000"  >&lt;p&gt;did some quick tests and looks like indeed we are not closing cursor when limit is involved.&lt;br/&gt;
tracking down bug.&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|hrgitj:</customfieldvalue>

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