Mongo Connection Pooling(Changing the size of connection pool)

六月ゝ 毕业季﹏ 提交于 2019-12-02 04:50:49

问题


How to change the mongo connection pool size?

I have seen it is 100 by default. Is there a way to change this value?

I dont want to do it via spring, is there a way to configure it via MongoClient?

There is an option i see about mongoClientOptions but i dont see options to set connection pool


回答1:


You can build your own MongoClient instance using the MongoClientOptions.Builder.

MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
MongoClientOptions options = builder.connectionsPerHost(10).build();
MongoClient client = new MongoClient(listOfServers, options);



回答2:


As another option(and more convenient for me), connection pool size can be changed via MongoDb URI. Sample:

 MONGODB_URI (mongo):   mongodb://user:password@localhost:27017/users_db?ssl=true&maxPoolSize=10&readPreference=primaryPreferred&replicaSet=Dev-shard-0&authSource=admin

Where maxPoolSize=10 param is max amount of connections. There are also some additional parameters for configuring connection pool in such way, for details - refer to the documentation - https://docs.mongodb.com/manual/reference/connection-string/




回答3:


Extra options can also be set using MongoClientURI :

MongoClientOptions.Builder builder = new MongoClientOptions.Builder().connectionsPerHost(10));
MongoClientURI clientURI = new MongoClientURI(connectionURL, builder);
MongoClient mongoClient = new MongoClient(clientURI);


来源:https://stackoverflow.com/questions/24557774/mongo-connection-poolingchanging-the-size-of-connection-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!