Reactive setup with Spring WebFlux , Kotlin, Postgres and Spring Data R2DBC

Overview

Reactive Spring with Kotlin and Pg

  • Spring WebFlux with Netty instead of Spring Web with Tomcat

  • Mono and Flux in all layers (controller, service, repo)

  • JDBC/JPA/Hibernate, Spring Data JDBC (javax.persistence) are non-reactive and hence not used for reactive approach

  • Two alternatives : 1. Spring Data R2DBC and 2. Reactive Hibernate


1. Spring Data R2DBC

  • Instead R2DBC(Reactive Relational Database Connectivity) and Spring Data R2DBC is used

  • No automatic table generation in R2DBC and less mature/ less features than JPA/Hibernate so flyway or liquibase are used for schema generation

  • Spring Data R2DBC provides ReactiveCrudRepository interface

  • org.springframework.data.relational.core.mapping.Table is used to indicate that a given class represents a table (does not create table in db, only representation )instead of javax.persistence.Table


repo

interface UserCredentialsRepo : ReactiveCrudRepository<UserCredentials, Int>

application.properties

spring.r2dbc.url=r2dbc:postgresql://localhost:5432/test
spring.r2dbc.username=postgres
spring.r2dbc.password=postgres
spring.data.r2dbc.repositories.enabled=true

pom.xml

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>


<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>


<dependency>
   <groupId>io.r2dbc</groupId>
   <artifactId>r2dbc-postgresql</artifactId>
   <scope>runtime</scope>
</dependency>

Limitations of Spring Data R2DBC :

  • doesn't deal with the relationship between entities itself . We will have to find a solution and do something manually

  • no support for auto-generated id


Spring Data R2DBC aims at being conceptually easy. In order to achieve this it does NOT offer caching, lazy loading, write behind or many other features of ORM frameworks. This makes Spring Data R2DBC a simple, limited, opinionated object mapper.

Spring Data R2DBC Not fully ready for every production project .

  • Projects without high concurrency issues don't need to use it
  • Some useful functions have not yet been realized ( For example, improving relationship management ). But they're in the team roadmap , Even if a clear release date has not yet been defined .Spring The team fully understands the needs of the developers , And working on it .

For all that , in some special cases , if the development team is ready to do more manual work , what is worth considering is R2DBC performance improvements.

References :
https://javamana.com/2021/01/20210120001209100Y.html
https://github.com/spring-projects/spring-data-r2dbc
https://dzone.com/articles/you-dont-need-hibernate-with-spring-webflux-and-r2


2. Hibernate Reactive

  • Hibernate started a subproject — Hibernate Reactive for Reactive Streams support

  • Spring still did not embrace Hibernate Reactive

  • We should deal with internals of Hibernate like SessionFactory, CriteriaBuilder, etc.

  • The reactive stream is represented using a chain of Java CompletionStages or Mutiny Unis and Multis ( alternatives of Mono and Flux )

  • Hibernate Reactive uses the reactive engine Vert.x and its non-blocking SQL client libraries (not R2DBC).


References :
https://www.i-programmer.info/news/80-java/15025-hibernate-goes-reactive-but-what-does-that-mean.html

You might also like...
Demo Spting REST Service on Kotlin. Works with PostgreSQL via Spring Data. Data initialization provided by liquibase

Spring Boot REST API with Kotlin Spring Boot REST API service. Spring Data with PostgreSQL. Data initialization with Liquibase. Swagger UI Reference D

A project to learn about Reactive Microservices experimenting with architectures and patterns
A project to learn about Reactive Microservices experimenting with architectures and patterns

reactive-microservices-workshop Copyright © 2021 Aleix Morgadas - Licenced under CC BY-SA 4.0 A project to learn about Reactive Microservices experime

Showcase project of Functional Reactive Programming on Android, using RxJava.

FunctionalAndroidReference FunctionalAndroidReference is a showcase project of Functional Reactive Programming on Android, using RxJava. It's a compan

Spring-kotlin - Learning API Rest with Kotlin, Spring and PostgreSQL

Kotlin, Spring, PostgreSQL and Liquibase Database Migrations Learning Kotlin for

This repository contains RabbitMQ Protobuf starters with its usage samples for spring-rabbit and spring-cloud-starter-stream-rabbit modules

This repository contains RabbitMQ Protobuf starters with its usage samples for spring-rabbit and spring-cloud-starter-stream-rabbit modules

Spring-with-maven - Spring Boot App with Postgresql and maven

Spring Boot Api Aplikasi ini dibuat menggunakan bahasa kotlin dan untuk database

Kotlin cli maven spring failsafe findbugs cucumber mockito junit car data

Kotlin cli maven spring failsafe findbugs cucumber mockito junit car data

Android login spring - Android login against spring backend

Android Jetpack Compose login implementation with JWT tokens against our own bac

A spring-boot project that demonstrates data caching using Redis

A spring-boot project that demonstrates data caching using Redis

Owner
Bimal Raj Gyawali
Learning to write maintainable and scalable servers.
Bimal Raj Gyawali
This repository demonstrates Spring GraphQL + RSocket + WebFlux + R2DBC + H2

Reactive GraphQL with Spring This repository demonstrates Spring GraphQL + RSocket + WebFlux + R2DBC + H2 O__ +-----------+

Maksim Kostromin 1 Nov 27, 2021
Spring Boot built using Kotlin, H2, Postgres, Hibernate and JPA

Spring-Boot-Kotlin-Sample Spring Boot built using Kotlin, H2, Postgres, Hibernate and JPA Getting Started Reference Documentation For further referenc

Reza Nur Rochmat 0 Jan 7, 2022
Liquibase R2DBC Spring Boot starter

This repository demonstrates how 2 implement Liquibase R2DBC Spring Boot starter. Stack: Liquibase R2DBC Spring Boot starter with next technology stack: Liquibase, R2DBC, Spring Boot, Kotlin, Junit Jupiter 5, MySQL, MariaDB, PostgreSQL, MS SQL Server, H2, r2dbc-pool, r2dbc-proxy, Test-containers, Maven, Bash

Maksim Kostromin 6 Dec 25, 2022
Simple Todo list API using Spring-Boot, Webflux, Kotlin, Coroutines, & PostgreSQL

Simple Todo list API using Spring-Boot, Webflux, Kotlin, Coroutines, & PostgreSQL

Sami Eljabali 9 Jul 26, 2022
Spring Boot Webflux RDBMS Sample App

R2DBC Spring Boot Webflux Sample Project generate schema (DDL script) h2-console (Web based database management console) WebFlux and r2dbc is not supp

Can Yaman 0 Nov 7, 2021
R2DBC Sharding Example with Kotlin Coroutine

R2DBC Sharding Example with Kotlin Coroutine A minimal sharding example with R2DBC and coroutine, where user table is divided into 2 different shards.

K.S. Yim 0 Oct 4, 2021
A sample skeleton backend app built using Spring Boot kotlin, Expedia Kotlin Graphql, Reactive Web that can be deployed to Google App Engine Flexible environmennt

spring-kotlin-gql-gae This is a sample skeleton of a backend app that was built using: Spring Boot(Kotlin) Reactive Web Sprinng Data R2DBC with MYSQL

Dario Mungoi 7 Sep 17, 2022
My personal template for a Spring Boot REST reactive webapp

My personal spring boot kotlin reactive template Features Spring Security implementation with JWT access and refresh token MongoDB database Project Co

Dušan 2 Dec 1, 2021
Spring-graphql-getting-started - Spring for GraphQL provides support for Spring applications built on GraphQL Java

Getting Started with GraphQL and Spring Boot Spring for GraphQL provides support

Shinya 0 Feb 2, 2022
Guide to setup JetBrains Projector and access Android Studio from any device

JetBrains Projector with Android Studio Guide to setup JetBrains Projector and access Android Studio from any device. Blog post Android Studio on an i

Joaquim Verges 326 Dec 15, 2022