0

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/).

Feng
  • 53
  • 9

2 Answers2

1

You can use connection pool monitoring which is described here to keep track of connection states, and deduce the counts you are looking for.

I don't know if Java driver exposes the counters you are looking for as public APIs; many drivers don't.

D. SM
  • 13,584
  • 3
  • 12
  • 21
  • looks a good way to go! Thanks for pointing this direction.If it works out, Ill come later and mark as the solution. – Feng Apr 13 '20 at 11:12
  • the com.mongodb.event.ConnectionPoolListener methods are, at the 4.0.1, all deprecated, and even when I tried to attach a sample class to the connection, I keep getting a java.lang.reflect.InvocationTargetException. I would like to stick with the 4.x version of the driver. – Feng Apr 14 '20 at 13:40
  • I am not sure what you are asking. Are you following published driver documentation and the provided code is not working? – D. SM Apr 14 '20 at 19:01
  • Precisely. It is throwing "java.lang.reflect.InvocationTargetException" (java.lang.reflect.InvocationTargetException: . . .ConnectionPoolListener.connectionPoolOpened" (but I didn't overwritten this method!). – Feng Apr 14 '20 at 21:04
  • You may want to create a new question where you reference the documentation, your code and the error message. – D. SM Apr 14 '20 at 23:33
0

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.

Feng
  • 53
  • 9