Search code examples
javamongodbmongodb-java

Get the number of open connections in mongoDB using java


My program requires a large number of connections to be open (Mongo). I get the error :

Too many connections open, can't open anymore

after 819 connections. I already know we can increase this limit. But that's not what I have in mind. I'm thinking of closing the MongoClient object, and then creating a new one again after 800 connections.

My thinking is that with a new mongoClient object all the connections will be closed and when I start/create it again, the connections will be opened again until 800. Thus not giving the error. (Let me know if this approach is totally wrong/ won't give the required results.)

For this I need to know the number of connections opened ATM. Is there any way to get this information using java?


Solution

  • You can get connection information by using the db.serverStatus() command. It has a connections subdocument which contains the total/available connections information.

    For more information :