Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-345

FileNotFoundException - too many open files

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 2.5.3
    • Component/s: API
    • Environment:
      Linux 64 Bit, Eclipse Helios Service Release 2

      I'm getting the following error:
      <<<
      java.io.FileNotFoundException: /home/user/image.jpg (Too many open files)
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.<init>(FileInputStream.java:106)
      at com.mongodb.gridfs.GridFS.createFile(GridFS.java:239)
      <<<

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

      File file = new File(filePath);
      GridFSInputFile inputFile = null;
      try {
      if(!file.exists())

      { throw new FileNotFoundException("File: " + filePath + " does not exist."); }

      inputFile = grid.createFile(file);
      inputFile.put("media", "file");
      inputFile.save();
      } catch (IOException e)

      { log.error("MedienDBWorker addFile() - ID: " + workID + " ******* ", e); }

      Is seams that GridFSInputFile is creating an InputStream and after saving the file, the InputStream isn't closed nor is there a method to close the stream.
      How should this be handled?

      My workaround is:
      File file = new File(filePath);
      GridFSInputFile inputFile = null;
      try {
      if(!file.exists())

      { throw new FileNotFoundException("File: " + filePath + " does not exist."); }

      final InputStream is = new FileInputStream(file);
      try

      { inputFile = grid.createFile(is,file.getName()); inputFile.put("media", "file"); inputFile.save(); }

      finally

      { is.close(); }


      } catch (IOException e)

      { log.error("MedienDBWorker addFile() - ID: " + workID + " ******* ", e); }

            Assignee:
            scotthernandez Scott Hernandez (Inactive)
            Reporter:
            chris_k Chris Kaempfe
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: