Search code examples
javamongodbconnection-pooling

How can I check for MongoDB's active connections status using Java driver


I have a requirement to publish (to Graphite) the 'number of active connection available' status of a Mongo db instance when a REST service is called. I know we can use db.serverStatus() to know the details of connections on the server side. I am looking to get the 'number of active connections available' information on the client side using JAVA API. The MongoDB Java Driver API documentation doesn't help much on it.


Solution

  • Assuming you are using the 3.0.x driver, and connecting to localhost on default port:

    MongoClient mongoClient = new MongoClient();
    MongoDatabase database = mongoClient.getDatabase("admin");
    Document serverStatus = database.runCommand(new Document("serverStatus", 1));
    Map connections = (Map) serverStatus.get("connections");
    Integer current = (Integer) connections.get("current");