title: Execute blocking JDBC call in Spring Webflux
body: <p>I am using Spring Webflux with Spring data jpa using PostgreSql as backend db.
  I don't want to block the main thread while making db calls like <code>find</code> and <code>save</code>.
  To achieve the same, I have a main scheduler in <code>Controller</code> class and a <code>jdbcScheduler</code> service classes.</p>
  
  <p>The way I have defined them is:</p>
  
  <pre><code>@Configuration
  @EnableJpaAuditing
  public class CommonConfig {
  
      @Value("${spring.datasource.hikari.maximum-pool-size}")
      int connectionPoolSize;
  
      @Bean
      public Scheduler scheduler() {
          return Schedulers.parallel();
      }
  
      @Bean
      public Scheduler jdbcScheduler() {
          return Schedulers.fromExecutor(Executors.newFixedThreadPool(connectionPoolSize));
      }
  
      @Bean
      public TransactionTemplate transactionTemplate(PlatformTransactionManager transactionManager) {
          return new TransactionTemplate(transactionManager);
      }
  }
  </code></pre>
  
  <p>Now, while doing a get/save call in my service layer I do:</p>
  
  <pre><code>    @Override
      public Mono&lt;Config&gt; getConfigByKey(String key) {
          return Mono.defer(
              () -&gt; Mono.justOrEmpty(configRepository.findByKey(key)))
              .subscribeOn(jdbcScheduler)
              .publishOn(scheduler);
      }
  
      @Override
      public Flux&lt;Config&gt; getAllConfigsAfterAppVersion(int appVersion) {
          return Flux
              .fromIterable(configRepository.findAllByMinAppVersionIsGreaterThanEqual(appVersion))
              .subscribeOn(jdbcScheduler)
              .publishOn(scheduler);
      }
  
      @Override
      public Flux&lt;Config&gt; addConfigs(List&lt;Config&gt; configList) {
          return Flux.fromIterable(configRepository.saveAll(configList))
              .subscribeOn(jdbcScheduler)
              .publishOn(scheduler);
      }
  </code></pre>
  
  <p>And in controller, I do:</p>
  
  <pre><code>    @PostMapping
      @ResponseStatus(HttpStatus.CREATED)
      Mono&lt;ResponseDto&lt;List&lt;Config&gt;&gt;&gt; addConfigs(@Valid @RequestBody List&lt;Config&gt; configs) {
          return configService.addConfigs(configs).collectList()
              .map(configList -&gt; new ResponseDto&lt;&gt;(HttpStatus.CREATED.value(), configList, null))
              .subscribeOn(scheduler);
      }
  </code></pre>
  
  <p>Is this correct? and/or there is a way better way to do it?</p>
  
  <p>What I understand by:</p>
  
  <pre><code>.subscribeOn(jdbcScheduler)
  .publishOn(scheduler);
  </code></pre>
  
  <p>is that task will run on <code>jdbcScheduler</code> threads and later result will be published on my main parallel <code>scheduler</code>. Is this understanding correct?</p>