我在这里发布问题只是为了确保我没有在错误的树上吠叫。
如何从客户端(例如Java客户端)的角度获取MongoDB的连接数 已使用(和免费)?
其他问题正在使用serverStatus(Get the number of open connections in mongoDB using java),但它假定对MongoDB具有“管理员”访问权限。使用“常规用户”(具有较低特权(例如,仅访问一个数据库)的数据库用户)无法运行serverStatus()。但这仅提供服务器端的视图(IP x中存在N个连接)。
[其他文章提到如何设置连接池大小(例如,使用MongoClients.create(MongoClientSettings settings)
(请参阅4.x API参考(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());
但是未提供任何方法来使连接池获得使用的和可用的连接。
您可以使用here中描述的连接池监视来跟踪连接状态,并推断出您要查找的计数。
我不知道Java驱动程序是否将您要查找的计数器公开为公共API;许多司机没有。