mongo java 驱动程序中的 getConnectionsPerHost() 和 getMaxSize() 方法有什么区别

what is the difference between getConnectionsPerHost() and getMaxSize() method in mongo java driver

我很难理解 getConnectionsPerHost()getMaxSize() 这两种方法的区别

public int getConnectionsPerHost() The maximum number of connections allowed per host for this MongoClient instance. Those connections will be kept in a pool when idle. Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection.

默认值为 100。

Returns: 每个主机连接池的最大大小

http://api.mongodb.com/java/current/com/mongodb/MongoClientOptions.html#getConnectionsPerHost--

public int getMaxSize()The maximum number of connections allowed. Those connections will be kept in the pool when idle. Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection.

默认为 100。 http://api.mongodb.com/java/current/com/mongodb/connection/ConnectionPoolSettings.html#getMaxSize--

也请详细说明如果 getConnectionsPerHost() 等于 10 并且 getMaxSize() 等于 10 那么这意味着什么以及我们如何确保连接池是一个?上面的方法有没有描述出来

getConnectionsPerHost() - 这表示与 MongoClient 相关的连接。

getMaxSize() - 这表示 mongodb 服务器的连接池大小。

通常 getConnectionsPerHost() 可以小于或等于 getMaxSize()。


具有内部连接池的 MongoDB 客户端 (MongoClient)。对于大多数应用程序,整个 JVM 应该有一个 MongoClient 实例。 http://api.mongodb.com/java/2.10.0/com/mongodb/MongoClient.html

MongoClient 连接池 连接池是由驱动程序维护的数据库连接的缓存,以便在需要与数据库的新连接时可以重新使用连接。为了减少应用程序创建的连接池的数量,建议调用一次 MongoClient.connect 并重新使用数据库变量进行进一步的数据库操作。 https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html

我们如何确保连接池是一个?上述任何方法是否描述它?

None 以上方法中的 getConnectionsPerHost() 或 getMaxSize() 不是单个连接池。

但是一个数据库集群只有一个 MongoClient 并在整个应用程序中使用它是一种很好的做法,因为 MongoClient class 被设计为线程安全的并且在线程之间共享。

创建多个 MongoClient 实例时,请记住所有资源使用限制(最大连接数等)适用于每个 MongoClient 实例并处理实例,确保调用 MongoClient.close() 进行清理资源