Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 989
Paul C Avatar asked Feb 27 '26 16:02

Paul C


1 Answers

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");
like image 63
Ori Dar Avatar answered Mar 01 '26 09:03

Ori Dar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!