3

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!

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
azhar_salati
  • 1,554
  • 5
  • 28
  • 54
  • what platform are you on? You should look at your open sockets as you use this. If you're on a unix-ish system, use netstat. Check out how many connections are open to where and what state they're in. – nojo Feb 18 '11 at 15:43
  • On linux you can see the number of open sockets with `netstat -p` on windows there should be something similar – Peter Lawrey Feb 18 '11 at 15:44

1 Answers1

3

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

Sven
  • 166
  • 4