1

I am trying the reactive stack with MySql at the backend. I was expecting that query results will be a stream , t.e. a heavy query will not wait and return the result when the all the records are found, but will stream them one by one. It does not look that way. The process waits untill the query is done and then returns all the results.

This is the Spring Data repository that I created:

public interface ScopusSpringDataRepo extends ReactiveCrudRepository<Scopus, Long> {
    @Query("select" + "  SC1.*" + "from Scopus as SC1  join Scopus as SC2"
            + "    on SC1.norma like concat(SC2.norma, '%') where ( SC2.norma is not null"
            + "  and SC2.word like concat('%', :word, '%'))")
    public Flux<Scopus> byNorma(String word);
}

This query is heavy on purpose.

Can someone please explain what behavior is expected from R2DBC in this case?

Thank you.

Update after the comments:

The repository method byNorma is called by a business service that is called by a REST Controller.

Service:

public class ScopusService {
protected ScopusSpringDataRepo scRepo;

public Flux<Scopus> getByNorma(String norma) {
    return scRepo.byNorma(norma);
}

}

REST Controller:

public class ScopusController {

protected ScopusService scopusService;

@GetMapping(path = "/stream/scopus/{word}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public ResponseEntity<Flux<Scopus>> getByNorma(@PathVariable String word) {
    log.info("Getby norma recieved. word :{}", word);
    return     ResponseEntity.ok(this.scopusService.getByNorma(word));
  }
}

Which is called from a Chromium browser.

Update:

I am trying to understand whether the R2DBC driver sends the result one by one or just gets all the results first and then sends them into the stream at once.

Kirill Kazoolin
  • 223
  • 1
  • 3
  • 12
  • Your server should return to the client before the query is executed. That is the benefit of reactive. – K.Nicholas Dec 13 '21 at 15:07
  • this impossible for us to answer as there can be other reasons around the code, that are blocking, or not returning things properly as a stream. – Toerktumlare Dec 13 '21 at 16:53
  • This is understanding that earlier DB apis were blocking IO and reactive r2dbc is providing the non blocking io. I am not sure if it gaurantees that it will stream the responses as and when its available. Would love to be educated on this. – Harry Dec 14 '21 at 00:26
  • How do you call `byNorma(String word)` method? – lkatiforis Dec 14 '21 at 11:34
  • Try adding some delay between the elements `.delayElements(Duration.ofSeconds(1))` to see what happens. – lkatiforis Dec 14 '21 at 17:47
  • I did add some delay in the service layer, and it did exactly what I expected - sending a member of the stream every second. But this happens after the DB fetch. I am trying to understand whether the R2DBC driver sends the result one by one or just gets all the results first and then sends them into the stream at once. – Kirill Kazoolin Dec 15 '21 at 08:24
  • R2DBC is backpressure-aware all the way down to the database. See my answer https://stackoverflow.com/a/70024213/8909235 – lkatiforis Dec 15 '21 at 09:27

0 Answers0