Solution for the reactive relational DB connection when programming using Spring WebFlux. When you want all you app to be reactive, don't make JDBC DB connection a bottlenack. Use R2DBC driver. In the presentation I share my experience on how to work with the driver, and if it is already ready to be used in serious projects on production. The talk was presented at Devoxx Ukraine, Nov 1, 2019.
9. R2DBC: Transactions
• SpringData handles "Start Transaction" and "Commit"/"Rollback"
automatically
• You're welcome to write your own (piece of code)
• Working without transactions is faster; do it if you can.
• Because of request and response in different threads, it's hard to
handle @Transactional in Spring
QueryResult queryResult = connectionPool
.inTransaction(conn ->
conn.sendQuery("INSERT INTO Person(NAME)
VALUES('sasha')")).get()
10. R2DBC and Spring Data
Simple queries like findOne() and saveAll() work well
BUT
@Query("SELECT * FROM USER WHERE USER_ID > :1 AND USER_ID < :2")
Flux<USERS> findByUserId(@Param("userId") Long userId,
@Param("userId") Long userIdSecond);
11. Is R2DBC better than JDBC?
• Both wait for result to return, even when this is a
List or Flux.
• BUT R2DBC uses NIO with its Event Loop =>
Request is sent and the thread becomes free.
Another thread is called to fetch the result.
14. Async and Non-blocking?
• JDK 9: new class java.util.concurrent.Flow -
Reactive Stream representation, standard for
asynchronous stream processing with non-
blocking back pressure.
• Assynchronous – calls you when is done
• Non-blocking – returns you a promise it will be
done, and you ask, if it’s ready.
• Aimed at runtime environments (JVM and
JavaScript) as well as network protocols.
Editor's Notes
WebFlux is the Spring Framework implementation of Reactive Streams. We see a rise in reactive programming models across our teams in general and the use of WebFlux in teams who are working in the Spring ecosystem. It's best used in large microservices ecosystems where the high performance of the requests is a major concern. It allows overlapping request processing asynchronously without the complications of using multiple threads. WebFlux uses Reactor as its reactive library but it is interoperable with other reactive libraries via Reactive Streams. It uses Netty as its underlying high-performance communications engine. Although we encourage using Reactive Streams, adopting this programming model requires a significant shift in thinking.
Reactive Streams - R2DBC is founded on Reactive Streams providing a fully reactive non-blocking API.
Relational Databases - R2DBC engages SQL databases with a reactive API, something not possible with the blocking nature of JDBC.
Scalable Solutions - Reactive Streams makes it possible to move from the classic one thread per connection approach to a more powerful, more scalable approach.
Open Specification - R2DBC is an open specification establishing a SPI that driver vendors can implement and clients can consume.