This document discusses building reactive database drivers with R2DBC. It covers how Spring supports reactive database access, an introduction to R2DBC including its design principles and SPI. It then discusses how R2DBC drivers work internally and provides examples of using R2DBC with Spring Data repositories. While R2DBC brings reactivity to database access, it is still immature and has limitations compared to blocking approaches like JPA.
1. / by Igor Lozynskyi
Building a
Reactive DB driver
with R2DBC
2. For whom this talk:
/ Want to build reactive apps?
/ Want to have reactive database access?
/ Want lo learn about R2DBC?
/ I assume, you know what Reactive is
18. ADBA
/ New async API for relational data access in Java
/ Implemented with CompletableFuture
/ Uses Java 9’s Flow API
19. Criticism
/ Does not imply back pressure
/ Has only ADBA-over-JDBC implementation
/ Slow development
/ Authors have neglected Reactive approach for a long time
/ No clear timeline
23. R2DBC Design principles
/ Based on Reactive Streams Types and Patterns
/ Be completely non-blocking, up to the DB
/ Utilize wire-protocol for non-blocking implementations
/ Divide Client API and Driver SPI
/ Shrink Driver SPI
41. Spring Data Repository
public interface UsSalesR2dbcRepository
extends R2dbcRepository<UsSalesDataDto, String> {
@Query("select * from us_sales_by_districts")
Flux<UsSalesDataDto> findAll();
@Query("select * from us_sales_by_districts, where code=:code")
Mono<UsSalesDataDto> findById(@Param("code") String code);
}
54. public interface UsSalesR2dbcRepository
extends R2dbcRepository<UsSalesDataDto, String> {
@Query("select * from us_sales_by_districts")
Flux<UsSalesDataDto> findAll();
@Query("select * from us_sales_by_districts, where code=:code")
Mono<UsSalesDataDto> findById(@Param("code") String code);
}
Spring Data R2DBC
64. R2DBC Pros
/ New and shiny
/ Brings reactive to DB access
/ More active community than ADBA’s
/ Easy to implement drivers (compared to JDBC)
65. R2DBC Cons
/ Not mature enough (current: 1.0.0.M7)
/ May be beaten by ADBA
/ No JPA (Hibernate/EclipseLink)
/ Reactive approach may not fit SQL at all
66. Summary
/ ADBA vs R2DBC battle is still going on
/ May soon have reactive DB access
/ Spring Data drives R2DBC
/ Not for production yet!
/ Don’t afraid to try R2DBC!