Java Mongo Connection Pooling(改变连接池的大小)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24557774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:58:45  来源:igfitidea点击:

Mongo Connection Pooling(Changing the size of connection pool)

javaspringmongodbmongo-javaspring-mongo

提问by neoeahit

How to change the mongo connection pool size?

如何更改 mongo 连接池大小?

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

我已经看到它默认为 100。有没有办法改变这个值?

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

我不想通过 spring 来做,有没有办法通过 MongoClient 配置它?

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

我看到了一个关于 mongoClientOptions 的选项,但我没有看到设置连接池的选项

采纳答案by evanwong

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

您可以使用MongoClientOptions.Builder构建自己的 MongoClient 实例。

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

回答by Alex Efimov

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

作为另一种选择(对我来说更方便),可以通过 MongoDb URI 更改连接池大小。样本:

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

Where maxPoolSize=10param 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/

其中maxPoolSize=10param 是最大连接数。还有一些额外的参数用于以这种方式配置连接池,有关详细信息 - 请参阅文档 - https://docs.mongodb.com/manual/reference/connection-string/

回答by Bit-Man

Extra options can also be set using MongoClientURI :

还可以使用 MongoClientURI 设置额外的选项:

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