Spring Boot和Mongo DB中的连接池 [英] Connection pooling in Spring Boot and mongo db

查看:93
本文介绍了Spring Boot和Mongo DB中的连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Spring Boot应用程序和mongoDb连接POC. 我添加了以下依赖项:

I am going through spring boot application and mongoDb connection POC. I have added following dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

我还遍历了具有以下属性的mongoB属性: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

Also I have gone through mongoB properties with properties: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

您能在这里定义连接池机制吗?

Can you please how do we define connection pooling mechanism here?

推荐答案

您无法使用应用程序属性立即执行此操作.您需要使用MongoClientOptions来配置连接池的各个方面.

You cannot do this out of the box with application properties. You need to make use of MongoClientOptions to configure various aspects of connection pool.

看看文档以获取各种可用选项.

Have a look at the documentation for various options available.

这是一个简单的例子.

Here is a simple example.

@Bean(name="mongoTempl")
public MongoTemplate mongoTempl() throws Exception {
     return new MongoTemplate(createMongoClient(new ServerAddress(host, port))
                              ,dbName);
}


Mongo createMongoClient(ServerAddress serverAddress) {
final MongoClientOptions options = MongoClientOptions.builder()
        .threadsAllowedToBlockForConnectionMultiplier(...)
        .connectionsPerHost(...)
        .connectTimeout(...)
        .maxWaitTime(...)
        .socketKeepAlive(...)
        .socketTimeout(...)
        .heartbeatConnectTimeout(...)
        .minHeartbeatFrequency(...)
        .build();

        return new MongoClient(serverAddress, options);
}

这篇关于Spring Boot和Mongo DB中的连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆