为什么Spring与Mongo使用了这么多连接

为什么Spring与Mongo使用了这么多连接,spring,mongodb,project-reactor,Spring,Mongodb,Project Reactor,我得到了异常“MongoWaitQueueFullException”,我知道我的应用程序正在使用的连接数。我使用SpringBoot的默认配置(2.2.7.RELEASE)和反应式MongoDB(4.2.8)。使用事务 即使在运行集成测试时,基本上创建了200多个元素,然后将它们分组(200组)使用10个连接。在实际数据集上执行此算法时,会引发此异常。已达到等待队列(500)的默认限制。这不会使应用程序具有可伸缩性 我的问题是:有没有办法设计一个反应式应用程序来帮助减少连接数? 这是我测试的输

我得到了异常“MongoWaitQueueFullException”,我知道我的应用程序正在使用的连接数。我使用SpringBoot的默认配置(2.2.7.RELEASE)和反应式MongoDB(4.2.8)。使用事务

即使在运行集成测试时,基本上创建了200多个元素,然后将它们分组(200组)使用10个连接。在实际数据集上执行此算法时,会引发此异常。已达到等待队列(500)的默认限制。这不会使应用程序具有可伸缩性

我的问题是:有没有办法设计一个反应式应用程序来帮助减少连接数?

这是我测试的输出。基本上,它扫描捆绑文件的所有翻译,并根据翻译密钥对它们进行分组。每个翻译键保留一个元素

        return Flux
            .fromIterable(bundleFile.getFiles())
            .map(ScannedBundleFileEntry::getLocale)
            .flatMap(locale ->
                    handler
                            .scanTranslations(bundleFileEntity.toLocation(), locale, context)
                            .index()
                            .map(indexedTranslation ->
                                    createTranslation(
                                            workspaceEntity,
                                            bundleFileEntity,
                                            locale.getId(),
                                            indexedTranslation.getT1(), // index
                                            indexedTranslation.getT2().getKey(), // bundle key
                                            indexedTranslation.getT2().getValue() // translation
                                    )
                            )
                            .flatMap(bundleKeyTemporaryRepository::save)
            )
            .thenMany(groupIntoBundleKeys(bundleFileEntity))
            .then(bundleKeyTemporaryRepository.deleteByBundleFile(bundleFileEntity.getId()))
            .then(Mono.just(bundleFileEntity));
分组功能:

  private Flux<BundleKeyEntity> groupIntoBundleKeys(BundleFileEntity bundleFile) {
    return this
            .findBundleKeys(bundleFile)
            .groupBy(BundleKeyGroupKey::new)
            .flatMap(bundleKeyGroup ->
                    bundleKeyGroup
                            .collectList()
                            .map(bundleKeys -> {
                                final BundleKeyGroupKey key = bundleKeyGroup.key();

                                final BundleKeyEntity entity = new BundleKeyEntity(key.getWorkspace(), key.getBundleFile(), key.getKey());
                                bundleKeys.forEach(entity::mergeInto);

                                return entity;
                            })
            )
            .flatMap(bundleKeyEntityRepository::save);
  }

您应该能够在客户端选项中的某个位置指定最大连接池大小。确实可以,但对于小数据集,它会消耗大量连接。这让我觉得有些地方不正确。连接数与数据集大小无关。您应该能够在客户端选项中的某个位置指定最大连接池大小。确实如此,但对于小数据集,它会消耗大量连接。这让我觉得有些地方不正确。连接数与数据集大小无关。
560  [main] INFO  o.s.b.t.c.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [be.sgerard.i18n.controller.TranslationControllerTest], using SpringBootContextLoader 
569  [main] INFO  o.s.t.c.s.AbstractContextLoader - Could not detect default resource locations for test class [be.sgerard.i18n.controller.TranslationControllerTest]: no resource found for suffixes {-context.xml, Context.groovy}. 
870  [main] INFO  o.s.b.t.c.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener, org.springframework.security.test.context.support.ReactorContextTestExecutionListener] 
897  [main] INFO  o.s.b.t.c.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@4372b9b6, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@232a7d73, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@4b41e4dd, org.springframework.test.context.support.DirtiesContextTestExecutionListener@22ffa91a, org.springframework.test.context.transaction.TransactionalTestExecutionListener@74960bfa, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@42721fe, org.springframework.test.context.event.EventPublishingTestExecutionListener@40844aab, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@1f6c9cd8, org.springframework.security.test.context.support.ReactorContextTestExecutionListener@5b619d14, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@66746f57, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@447a020, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@7f36662c, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@28e8dde3, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@6d23017e] 
1551 [background-preinit] INFO  o.h.v.i.x.c.ValidationBootstrapParameters - HV000006: Using org.hibernate.validator.HibernateValidator as validation provider. 
1677 [main] INFO  b.s.i.c.TranslationControllerTest - Starting TranslationControllerTest on sgerard with PID 538 (started by sgerard in /home/sgerard/sandboxes/github-oauth/server) 
1678 [main] INFO  b.s.i.c.TranslationControllerTest - The following profiles are active: test 
3250 [main] INFO  o.s.d.r.c.RepositoryConfigurationDelegate - Bootstrapping Spring Data Reactive MongoDB repositories in DEFAULT mode. 
3747 [main] INFO  o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 493ms. Found 9 Reactive MongoDB repository interfaces. 
5143 [main] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.security.config.annotation.method.configuration.ReactiveMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.ReactiveMethodSecurityConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
5719 [main] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} 
5996 [cluster-ClusterId{value='5f42490f1c60f43aff9d7d46', description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:4337}] to localhost:27017 
6010 [cluster-ClusterId{value='5f42490f1c60f43aff9d7d46', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 8]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=12207332, setName='rs0', canonicalAddress=4802c4aff450:27017, hosts=[4802c4aff450:27017], passives=[], arbiters=[], primary='4802c4aff450:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000013, setVersion=1, lastWriteDate=Sun Aug 23 12:46:30 CEST 2020, lastUpdateTimeNanos=384505436362981} 
6019 [main] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} 
6040 [cluster-ClusterId{value='5f42490f1c60f43aff9d7d47', description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:2, serverValue:4338}] to localhost:27017 
6042 [cluster-ClusterId{value='5f42490f1c60f43aff9d7d47', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 8]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1727974, setName='rs0', canonicalAddress=4802c4aff450:27017, hosts=[4802c4aff450:27017], passives=[], arbiters=[], primary='4802c4aff450:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000013, setVersion=1, lastWriteDate=Sun Aug 23 12:46:30 CEST 2020, lastUpdateTimeNanos=384505468960066} 
7102 [nioEventLoopGroup-2-2] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:3, serverValue:4339}] to localhost:27017 
11078 [main] INFO  o.s.b.a.e.web.EndpointLinksResolver - Exposing 1 endpoint(s) beneath base path '' 
11158 [main] INFO  o.h.v.i.x.c.ValidationBootstrapParameters - HV000006: Using org.hibernate.validator.HibernateValidator as validation provider. 
11720 [main] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:4, serverValue:4340}] to localhost:27017 
12084 [main] INFO  o.s.s.c.ThreadPoolTaskScheduler - Initializing ExecutorService 'taskScheduler' 
12161 [main] INFO  b.s.i.c.TranslationControllerTest - Started TranslationControllerTest in 11.157 seconds (JVM running for 13.532) 

20381 [nioEventLoopGroup-2-3] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:5, serverValue:4341}] to localhost:27017 
20408 [nioEventLoopGroup-2-2] INFO  b.s.i.s.w.WorkspaceManagerImpl - Synchronize, there is no workspace for the branch [master], let's create it. 
20416 [nioEventLoopGroup-2-3] INFO  b.s.i.s.w.WorkspaceManagerImpl - The workspace [master] alias [e3cea374-0d37-4c57-bdbf-8bd14d279c12] has been created. 
20421 [nioEventLoopGroup-2-3] INFO  b.s.i.s.w.WorkspaceManagerImpl - Initializing workspace [master] alias [e3cea374-0d37-4c57-bdbf-8bd14d279c12]. 
20525 [nioEventLoopGroup-2-2] INFO  b.s.i.s.i18n.TranslationManagerImpl - A bundle file has been found located in [server/src/main/resources/i18n] named [exception] with 2 file(s). 
20812 [nioEventLoopGroup-2-4] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:6, serverValue:4342}] to localhost:27017 
21167 [nioEventLoopGroup-2-8] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:10, serverValue:4345}] to localhost:27017 
21167 [nioEventLoopGroup-2-6] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:8, serverValue:4344}] to localhost:27017 
21393 [nioEventLoopGroup-2-5] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:4343}] to localhost:27017 
21398 [nioEventLoopGroup-2-7] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:9, serverValue:4346}] to localhost:27017 
21442 [nioEventLoopGroup-2-2] INFO  b.s.i.s.i18n.TranslationManagerImpl - A bundle file has been found located in [server/src/main/resources/i18n] named [validation] with 2 file(s). 
21503 [nioEventLoopGroup-2-2] INFO  b.s.i.s.i18n.TranslationManagerImpl - A bundle file has been found located in [server/src/test/resources/be/sgerard/i18n/service/i18n/file] named [file] with 2 file(s). 
21621 [nioEventLoopGroup-2-2] INFO  b.s.i.s.i18n.TranslationManagerImpl - A bundle file has been found located in [front/src/main/web/src/assets/i18n] named [i18n] with 2 file(s). 

22745 [SpringContextShutdownHook] INFO  o.s.s.c.ThreadPoolTaskScheduler - Shutting down ExecutorService 'taskScheduler' 
22763 [SpringContextShutdownHook] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:4, serverValue:4340}] to localhost:27017 because the pool has been closed. 
22766 [SpringContextShutdownHook] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:9, serverValue:4346}] to localhost:27017 because the pool has been closed. 
22767 [SpringContextShutdownHook] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:6, serverValue:4342}] to localhost:27017 because the pool has been closed. 
22768 [SpringContextShutdownHook] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:8, serverValue:4344}] to localhost:27017 because the pool has been closed. 
22768 [SpringContextShutdownHook] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:5, serverValue:4341}] to localhost:27017 because the pool has been closed. 
22769 [SpringContextShutdownHook] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:10, serverValue:4345}] to localhost:27017 because the pool has been closed. 
22770 [SpringContextShutdownHook] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:7, serverValue:4343}] to localhost:27017 because the pool has been closed. 
22776 [SpringContextShutdownHook] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:3, serverValue:4339}] to localhost:27017 because the pool has been closed. 

Process finished with exit code 0