为什么Spring和Mongo会使用这么多的连接

问题描述

我得到了异常'MongoWaitQueueFullException',我意识到我的应用程序正在使用的连接数。我将Spring Boot(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],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',roundtripTimeNanos=1727974,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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)