Search code examples
mongodbconnection-pooling

How to get the number of connections used (and free) to the MongoDB (from a client perspective)?


I'm posting the question here just to be sure I'm not barking on the wrong tree.

How to get the number of connections used (and free) to the MongoDB, but from a client perspective (eg. Java client), using the 4.x driver?

There are posts regarding using the serverStatus(Get the number of open connections in mongoDB using java), but it presumes having 'admin' access to the MongoDB. Using a 'regular user'(an db user with lower privileges (e.g access to only one database)) cannot run the serverStatus(). But this provides only a view from the server-side (there are N connections from IP x).

Other posts mentioned how to setup the connection pool size (eg. using the MongoClients.create​(MongoClientSettings settings) (see the 4.x API reference (https://mongodb.github.io/mongo-java-driver/4.0/apidocs/mongodb-driver-sync/com/mongodb/client/MongoClients.html)):

        MongoCredential credential = MongoCredential.createCredential(
                    username,
                    "admin",
                    password.toCharArray());

        MongoClient mongoClient = MongoClients.create(MongoClientSettings.builder()
                .applyToClusterSettings(
                        builder -> builder.hosts(Arrays.asList(new ServerAddress(hostname, portNumber))))
                .credential(credential)
                .applyToConnectionPoolSettings(builder -> builder
                                                                .minSize(connectionPoolMinimumSize)
                                                                .maxSize(connectionPoolMaximumSize))
                .readConcern(readConcern)
                .readPreference(readPreference)
                .writeConcern(writeConcern)
                .build());

But none provided means to get the used and available connections the connection pool.

As mentioned by Oleg, using the ConnectionPoolListener would be a way, but that is available only in the 3.x drivers. The ConnectionPoolListener methods are marked as deprecated on 4.x (although it is still mentioned in the JMX Monitoring section (http://mongodb.github.io/mongo-java-driver/4.0/driver-reactive/reference/monitoring/).


Solution

  • Finally got this working:

    • created a custom connection pool listener, implementing the com.mongodb.event.ConnectionPoolListener...
        public class CustomConnectionPoolListener implements ConnectionPoolListener {
            ...
        }
    
    • ... and having the stats counters updated on a store (accessible later)
            @Override
            public void connectionCreated(ConnectionCreatedEvent event) {
                ConnectionPoolStatsPOJO cps = mongoConnectionPoolList.get(connectionPoolAlias);
                cps.incrementConnectionsCreated();
                mongoConnectionPoolList.put(connectionPoolAlias, cps);
            }
    
    • attached this custom connection pool listener to the MongoClient connection:
    ConnectionPoolListener customConnPoolListener = new CustomConnectionPoolListener(...); /* added some references in the */
        ...
        MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                    .applicationName(applicationName)
                    .applyConnectionString(connURI)
                    .credential(credential)
                    .readConcern(readConcern)
                    .readPreference(readPreference)
                    .writeConcern(writeConcern)
                    .applyToConnectionPoolSettings(builder -> builder
                            .minSize(connectionPoolMinimumSize)
                            .maxSize(connectionPoolMaximumSize)
                            .addConnectionPoolListener(customConnPoolListener)
                    )
                    .retryWrites(true)
                    .retryReads(true)
                    .build();
        ...
        MongoClient mongoClient = MongoClients.create(mongoClientSettings);
        ....
    
    • finally, to access the connection pool stats, just have to query out the store:
            ConnectionPoolStatsPOJO connectionPoolStats = MongoDB_ConnectionPool_Repository.getInstance().getMongoConnectionPoolList().get(connectionPoolAlias);
    

    Therefore, thanks to "@D. SM" for pointing to the right direction.