Java Database Connectivity (JDBC) API is still blocking and synchronous. There are proposals to make it fully non-blocking and asynchronous, but that work is still in progress. This means, as of writing this book, there is no way to do reactive programming if you persist to a database using JDBC.
But there is a small caveat that can be used to mimic reactive behavior, you can use a pool of threads to execute synchronous tasks asynchronously.
The following DbConfig class is used to do just that:
@Configuration
public class DbConfig {
private Integer connectionPoolSize;
public DbConfig(@Value("${spring.datasource.maximum-pool-size:10}")
Integer connectionPoolSize) {
this.connectionPoolSize = connectionPoolSize;
}
@Bean
public Scheduler dbScheduler() {
return Schedulers.fromExecutor(Executors.newFixedThreadPool
(connectionPoolSize));
}
}
The dbScheduler() method creates a Scheduler bean, which encapsulates a standard Java fixed-size thread pool for executive tasks. This bean will be used in the service layer, as explained in the next section.