A couple of years ago, Reactive Programming was all the rage, but it had one big issue: reactive stopped as soon as you accessed a SQL database. You had a nice reactive chain up to the database, defeating the whole purpose. Given the prevalence of SQL databases in existing and new apps, one couldn't enjoy the full benefits of Reactive Programming but still pay the full price of complexity.
Since then, the landscape has changed tremendously. Most importantly, it offers many reactive drivers over popular databases: PostgreSQL, MariaDB and MySQL, Microsoft SQL Server, Oracle, you name it! Even better, some frameworks provide a reactive API over them.
Even though I'm not providing consulting services regularly, I wanted to keep up-to-date on accessing data reactively. In this post, I'll describe Hibernate Reactive, Spring Data R2DBC, and jOOQ in no particular order.
The base application uses Project Reactor and its types - Flux and Mono. For an added twist, I use Kotlin (without coroutines). Most code snippets have unnecessary type hints for better understanding.
The demo model
I don't want a complicated demo model, but I don't want it to be too simple. I'll use a single many-to-many relationship and a field with LocalDate:
Spring Data R2DBC
As far as I remember, the Spring ecosystem was the first to offer a reactive database access API. At first, it was limited to H2 - not very useful in production. However, new reactive drivers were easy to integrate.
Spring Data RDBC builds upon the widespread Spring Data JPA. The biggest difference is that there's a single required annotation for entities, @Id.
Here's the code for the person table:
data classPerson(@Idvalid:Long,valfirstName:String,vallastName:String,valbirthdate:LocalDate?,@Transientvaladdresses:MutableSet<Address>=mutableSetOf())interfacePersonRepository:ReactiveCrudRepository<Person,Long>
R2DBC repositories look similar to regular Spring Data repositories with one big difference. They integrate Project Reactor's reactive types, Mono and Flux. Note that it's easy to use Kotlin's coroutines with an additional bridge dependency.
Now comes the hard problem: mapping the many-to-many relationship with the Address.
First, we must tell Spring Data R2DBC to use a specific constructor with an empty set of addresses.
data classPerson(@Idvalid:Long,valfirstName:String,vallastName:String,valbirthdate:LocalDate?,@Transientvaladdresses:MutableSet<Address>=mutableSetOf()){@PersistenceCreatorconstructor(id:Long,firstName:String,lastName:String,birthdate:LocalDate?=null):this(id,firstName,lastName,birthdate,mutableSetOf())}
We also need to define the Address repository, as well as a query to list all addresses of a person:
interfaceAddressRepository:ReactiveCrudRepository<Address,Long>{@Query("SELECT * FROM ADDRESS WHERE ID IN (SELECT ADDRESS_ID FROM PERSON_ADDRESS WHERE PERSON_ID = :id)")funfindAddressForPersonById(id:Long):Flux<Address>}
Now comes the least tasteful part: Spring Data R2DBC doesn't support many-to-many relationships at the moment. We need a hook that queries the addresses after loading a person.
Annotate with @Lazy to avoid running into circular dependencies exception during injection
Use the above query
Add each address
Reactive trick to wait for the last bit of data
Turn into a single Person
As far as I can understand, Spring Data R2DBC still needs to execute additional queries, thus leading to the (in)famous N+1 query problem.
One configures database access via all available Spring alternatives: properties, YAML, Spring profiles, environment variables, etc. Here's a YAML example:
The only difference so far from the regular Hibernate configuration
Here's the source for the query itself:
valemf=Persistence.createEntityManagerFactory("postgresql")//1valsessionFactory:Mutiny.SessionFactory=emf.unwrap(Mutiny.SessionFactory::class.java)//2valpeople:Mono<MutableList<Person>>=sessionFactory.withSession{it.createQuery<Person>("SELECT p FROM Person p LEFT JOIN FETCH p.addresses a").resultList}.convert().with(UniReactorConverters.toMono())//3
Regular EntityManagerFactory
Unwrap the underlying session factory implementation. Because we configured a ReactivePersistenceProvider in the persistence.xml, it's a Mutiny.SessionFactory
Hibernate Reactive integrates with Vert.x, but an extension allows to bridge to Project Reactor if wanted
Note that Hibernate Reactive is the only library among the three to return a Mono<List<T>> instead of a Flux<T>. In layman's terms, it means you get the whole list at once instead of getting the elements one by one and being able to do something on each one individually.
jOOQ Reactive
As for the two above frameworks, jOOQ Reactive is similar to its non-reactive version. You first generate the code from the database schema, then use it.
The version is defined in the parent Spring Boot Starter parent POM
Set the necessary database driver(s). Note that one should use the non-reactive driver
There's a Kotlin generator!
Configure database configuration parameters
Once you've generated the code, you can create your data class and design the query. jOOQ class hierarchy integrates with Java's collections, Java's Reactive Streams, and Project Reactor.
The code may look complex if you're neither a SQL nor a jOOQ expert.
Remember that variable types are unnecessary, but added for documentation purposes:
Convert the row to an ordinary Java object via a function
The magic happens here: wrap the regular query in a Flux for people is a Project Reactor Publisher
Let's dive a bit into multiset from the point of view of a non-jOOQ expert - me. Initially, I tried to execute a regular SQL query with results I tried to flatten with Project Reactor's API. I failed miserably because of my lack of knowledge of the API, but even if I had succeeded, it would have been the wrong approach.
The MULTISET value constructor is one of jOOQ's and standard SQL's most powerful features. It allows for collecting the results of a non scalar subquery into a single nested collection value with MULTISET semantics.
In the above query, we first select all addresses of a person, map each row to an object, and flatten them in a list on the same result row as the person. The second mapper maps the row, including the address list, to a dedicated person with an addresses list.
I'm not a SQL master, so multiset is hard at first glance. However, I confirm that it's a powerful feature indeed.
Note that nested collections are fetched eagerly on a per-record basis, whereas top-level records are streamed reactively.
Conclusion
We have browsed the surface of the main three reactive database access: Spring Data R2DBC, Hibernate, and jOOQ. So, which one should one choose?
The main deciding factor is whether you already use one of their non-reactive flavors. Use the framework you're familiar with since both reactive and non-reactive usages are similar.
I think that jOOQ is extremely powerful but requires a familiarity I currently lack. If you have complex queries that don't map easily to other approaches, it's the way to go. Besides that, I've no strong opinion, though I find Hibernate Reactive's configuration too limited by JPA and its Mono<List<T>> return type puzzling.
Thanks for Lukas Eder and Mark Paluch for their reviews on their respective sections of expertise.
The complete source code for this post can be found on GitHub: