Search code examples
javamongodbmongo-java

mongo + java + too many open files


I am using mongo-java2.4jar for communicating with the mongo server. In my webapp i am using mongo=new Mongo("serverIp","port") where ever it is required and once the processing is complete, I am closing the mongo connection using mongo.close().

But after some time I am getting following exception :

java.net.SocketException: Too many open files

I think when I close the connection it is not closing the sockets. Please help me out figuring this issue.

Thanks!


Solution

  • The Mongo class transparently does connection pooling and you should generally have only one instance per JVM process. Please look at http://api.mongodb.org/java/2.5-pre-/com/mongodb/Mongo.html

    If you heavily create instances of this class i think you will acquire too many connections before they can be released. Just create a singleton on app startup for your whole application and place it in the application context. Call close only when your app stops.

    Cheers,

    Sven