【问题标题】:Spring Weblflux not saving all objects in DBSpring Webflux 不保存数据库中的所有对象
【发布时间】:2021-02-02 17:55:15
【问题描述】:

在下面的代码中,标记为 //1 和 //2 的 retrivedPrev 和 retrivedNext 的保存不会使用 Mongo 响应式保存,并且控件仅保存标记为 //3 的 retrievedPortCall。请建议我如何使用 Spring Webflux 实现相同的目标。如果我使用 block(),我会得到 Exception - block() is blocking which is not supported by thread reactor http-nio-2

public Mono<PortCall> updateByFindById(String gsisKey, PortCall portCall) {
    
    PortCall nextPort = portCallRepository.findById(portCall.getNextPortCall().getNextScheduleEntryKey()).toProcessor().block();

    PortCall prevPort = portCallRepository.findById(portCall.getPreviousPortCall().getPreviousScheduleEntryKey()).toProcessor().block();

    Mono<PortCall> prev = portCallRepository.findById(portCall.getPreviousPortCall()
            .getPreviousScheduleEntryKey()).flatMap(retrivedPrev -> {
                                    
                retrivedPrev.getNextPortCall().setNextScheduleEntryKey(nextPort.getGsisKey());
                retrivedPrev.getNextPortCall().setTerminalCode(nextPort.getSiteRkstCode());
                return portCallRepository.save(retrivedPrev);   //1
            });
    
     Mono<PortCall> next = portCallRepository.findById(portCall.getNextPortCall()
              .getNextScheduleEntryKey()).flatMap(retrivedNext->{
                
                 retrivedNext.getPreviousPortCall().setPreviousScheduleEntryKey(prevPort.getGsisKey());
                 retrivedNext.getPreviousPortCall().setTerminalCode(prevPort.getSiteRkstCode());
                return portCallRepository.save(retrivedNext);  //2
              });   
     
    return portCallRepository.findById(gsisKey)
            .flatMap(retrivedPortCall -> {
                    retrivedPortCall.setSiteCallStatus(SiteCallStatus.DELETED);
                    return portCallRepository.save(retrivedPortCall)  //3
                });
}

【问题讨论】:

  • 我看不到你是否使用@Transactional注解。
  • 这是 mongo Db ,在 mongo 我们不使用@Transactional,还有一个对象被保存,只有其他 2 个没有被保存
  • 我认为你没有订阅那些反应对象,儿子这就是为什么他们什么都不做。您可以在 return 语句中将它们全部链接起来。
  • @RafaelGuillen:我该怎么做,你能帮忙吗?
  • 这个答案应该可以帮助你...stackoverflow.com/questions/60836450/…

标签: java-8 reactive-programming spring-webflux


【解决方案1】:

根据我的上一条评论,您的代码应如下所示:

public Mono<PortCall> updateByFindById(String gsisKey, PortCall portCall) {
    
    PortCall nextPort = portCallRepository.findById(portCall.getNextPortCall().getNextScheduleEntryKey()).toProcessor().block();

    PortCall prevPort = portCallRepository.findById(portCall.getPreviousPortCall().getPreviousScheduleEntryKey()).toProcessor().block();

    Mono<PortCall> prev = portCallRepository.findById(portCall.getPreviousPortCall()
            .getPreviousScheduleEntryKey()).flatMap(retrivedPrev -> {
                                    
                retrivedPrev.getNextPortCall().setNextScheduleEntryKey(nextPort.getGsisKey());
                retrivedPrev.getNextPortCall().setTerminalCode(nextPort.getSiteRkstCode());
                return portCallRepository.save(retrivedPrev);   //1
            });
    
     Mono<PortCall> next = portCallRepository.findById(portCall.getNextPortCall()
              .getNextScheduleEntryKey()).flatMap(retrivedNext->{
                
                 retrivedNext.getPreviousPortCall().setPreviousScheduleEntryKey(prevPort.getGsisKey());
                 retrivedNext.getPreviousPortCall().setTerminalCode(prevPort.getSiteRkstCode());
                return portCallRepository.save(retrivedNext);  //2
              });   
     
    Mono<PortCall> gsis = portCallRepository.findById(gsisKey)
            .flatMap(retrivedPortCall -> {
                    retrivedPortCall.setSiteCallStatus(SiteCallStatus.DELETED);
                    return portCallRepository.save(retrivedPortCall)  //3
                });
    return Mono.zip(prev, next, gsis).map(value -> tuple.getT3());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 2018-06-15
    • 2018-05-30
    相关资源
    最近更新 更多