I am not sure if I am doing something wrong here but the below code in Spring WebFlux runs 2 times. I am using Spring Boot 2.3.3.RELEASE and MongoDB as database.
There are 3 repositories called: sessionRepo, userRepo, requestRepo
public Mono<CustomeResponse> createUpdateUser(Request request) {
return sessionRepo
.findBySid(request.getId())
.log()
.flatMap(sessionInfo -> userRepo.findById(sessionInfo.getUid())
.log()
.map(quoteDocument -> {
// other update user logic
userRepo.save(user).subscribe();
return success;
})
.onErrorReturn().defaultIfEmpty())
.switchIfEmpty(createNewUser(request))
.onErrorReturn());
}
public Mono<CustomeResponse> createNewUser(Request request){
return requestRepo.findById(request.getRequestInfo().getId())
.log()
.map(requestInfo -> {
// other create user logic
userRepo.save(user).subscribe();
return success;
})
.defaultIfEmpty()
.onErrorReturn();
}
I have noticed most of my code runs 2 times. But all the API’s where I must create new ID’s like session-id and send it to the Front end it always sends the second one creating a duplicate in the database.
Even switchIfEmpty runs 2 times.
Comment From: rstoyanchev
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
In addition I would suggest that you provide more details, ideally a sample. The code references multiple types that aren't available to see and it's hard to make out which parts run twice or what the method signatures are.