I am having difficulty in understanding the difference between getConnectionsPerHost()
and getMaxSize()
the two methods
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.
Default is 100.
Returns: the maximum size of the connection pool per host
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.
Default is 100. http://api.mongodb.com/java/current/com/mongodb/connection/ConnectionPoolSettings.html#getMaxSize--
please elaborate also if getConnectionsPerHost()
is equal to 10 and getMaxSize()
,is equal to 10 then what does it means and how we make sure that connection pool is one ? does any of the above methods describe it or not
getConnectionsPerHost() - This denotes the connection related to the MongoClient.
getMaxSize() - This denotes the Connection pool size of a mongodb server.
In general getConnectionsPerHost() can be less than or equal to getMaxSize().
A MongoDB client (MongoClient) with internal connection pooling. For most applications, you should have one MongoClient instance for the entire JVM. http://api.mongodb.com/java/2.10.0/com/mongodb/MongoClient.html
MongoClient connection pooling A Connection Pool is a cache of database connections maintained by the driver so that connections can be re-used when new connections to the database are required. To reduce the number of connection pools created by the application, it is recommended calling MongoClient.connect once and reusing the database variable for further DB operations. https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
How we make sure that connection pool is one ? does any of the above methods describe it or not ?
None of the above methods getConnectionsPerHost() or getMaxSize() doesnot single connection pool.
But it is a good practice to have only one MongoClient for an database cluster and use it across application, since the MongoClient class is designed to be thread safe and shared among threads.
When creating many MongoClient instances, keep in mind that all resource usage limits (max connections, etc) apply per MongoClient instance and to dispose of an instance, make sure you call MongoClient.close() to clean up resources