<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:52:04 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-345] FileNotFoundException - too many open files</title>
                <link>https://jira.mongodb.org/browse/JAVA-345</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;I&apos;m getting the following error: &lt;br/&gt;
&amp;lt;&amp;lt;&amp;lt;&lt;br/&gt;
java.io.FileNotFoundException: /home/user/image.jpg (Too many open files)&lt;br/&gt;
	at java.io.FileInputStream.open(Native Method)&lt;br/&gt;
	at java.io.FileInputStream.&amp;lt;init&amp;gt;(FileInputStream.java:106)&lt;br/&gt;
	at com.mongodb.gridfs.GridFS.createFile(GridFS.java:239)&lt;br/&gt;
&amp;lt;&amp;lt;&amp;lt;&lt;/p&gt;

&lt;p&gt;I&apos;m migrating a Oracle DB to MongoDB. For the work I&apos;m using several threads which reads Orcale and put the information to MongoDB.&lt;br/&gt;
Therefor im crawling a filesystem for different files, which need to be inserted in MongoDB.&lt;br/&gt;
My Code - within the thread - where I&apos;m receiving the exception:&lt;/p&gt;

&lt;p&gt;    File file = new File(filePath);&lt;br/&gt;
    GridFSInputFile inputFile = null;&lt;br/&gt;
    try {&lt;br/&gt;
            if(!file.exists()) &lt;/p&gt;
{
		throw new FileNotFoundException(&quot;File: &quot; + filePath + &quot; does not exist.&quot;);
	    }
&lt;p&gt;	   inputFile = grid.createFile(file);&lt;br/&gt;
	   inputFile.put(&quot;media&quot;, &quot;file&quot;);&lt;br/&gt;
	   inputFile.save();				&lt;br/&gt;
    } catch (IOException e) &lt;/p&gt;
{
	log.error(&quot;MedienDBWorker addFile() - ID: &quot; + workID + &quot; ******* &quot;, e);
    }

&lt;p&gt;Is seams that GridFSInputFile is creating an InputStream and after saving the file, the InputStream isn&apos;t closed nor is there a method to close the stream.&lt;br/&gt;
How should this be handled?&lt;/p&gt;

&lt;p&gt;My workaround is:&lt;br/&gt;
    File file = new File(filePath);&lt;br/&gt;
    GridFSInputFile inputFile = null;&lt;br/&gt;
    try {&lt;br/&gt;
            if(!file.exists()) &lt;/p&gt;
{
		throw new FileNotFoundException(&quot;File: &quot; + filePath + &quot; does not exist.&quot;);
	    }

&lt;p&gt;	    final InputStream is = new FileInputStream(file);&lt;br/&gt;
	    try &lt;/p&gt;
{
		inputFile = grid.createFile(is,file.getName());
		inputFile.put(&quot;media&quot;, &quot;file&quot;);
		inputFile.save();				
	    }
&lt;p&gt;	    finally &lt;/p&gt;
{
		is.close();				
	    }
&lt;p&gt;			&lt;br/&gt;
    } catch (IOException e) &lt;/p&gt;
{
	    log.error(&quot;MedienDBWorker addFile() - ID: &quot; + workID + &quot; ******* &quot;, e);
    }</description>
                <environment>Linux 64 Bit, Eclipse Helios Service Release 2</environment>
        <key id="16584">JAVA-345</key>
            <summary>FileNotFoundException - too many open files</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="2" iconUrl="https://jira.mongodb.org/images/icons/priorities/critical.svg">Critical - P2</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="chris_k">Chris Kaempfe</reporter>
                        <labels>
                            <label>GridFSInputFile</label>
                            <label>insert</label>
                    </labels>
                <created>Tue, 3 May 2011 12:06:45 +0000</created>
                <updated>Tue, 25 Jun 2013 16:51:13 +0000</updated>
                            <resolved>Tue, 3 May 2011 14:40:31 +0000</resolved>
                                    <version>2.5.3</version>
                                                    <component>API</component>
                                        <votes>0</votes>
                                    <watches>1</watches>
                                                                                                                <comments>
                            <comment id="30829" author="antoine" created="Tue, 3 May 2011 16:49:06 +0000"  >&lt;p&gt;thanks for the report we will add the close().&lt;br/&gt;
Another workaround may be to force gc() so that the file handles get finalized and closed.&lt;br/&gt;
Also make sure that you are using only 1 mongo instance for all threads: if you use 1 mongo per thread you may end up using too many connections.&lt;/p&gt;</comment>
                            <comment id="30792" author="scotthernandez" created="Tue, 3 May 2011 14:40:31 +0000"  >&lt;p&gt;This is a system issue with the number of open files and has nothing to do with the driver.&lt;/p&gt;

&lt;p&gt;You should increase the number of open file handles that your process is allowed to use; use ulimit -n to do this, or talk to your sysadmin.&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|hrhc5b:</customfieldvalue>

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