R2DBC: Reactive Relational Database Connectivity for Java and Spring

Home » Blog » Software » Enterprise Software » R2DBC: Reactive Relational Database Connectivity for Java and Spring

As I mentioned in a blog post last week with Reactor and Spring WebFlux reactive programming has come to Java and the Spring platform.

R2DBC is relatively new project that aims to “to bring a reactive programming API to relational data stores“.

While currently still considered experimental, R2DBC endeavours to introduce reactive programming to how we access relational databases (reactive relational, hence the R2 in the name). This means that instead of the traditional way of querying SQL databases imperatively with prepared statements you’ll be able to write code such as

r2dbc.inTransaction(handle ->
    handle.execute("INSERT INTO test VALUES ($1)", 100))

    .thenMany(r2dbc.inTransaction(handle ->
        handle.select("SELECT value FROM test")
            .execute(result -> result.map((row, rowMetadata) -> row.get("value", Integer.class)))))

    .subscribe(System.out::println);

This’ll allow you to describe the desired behaviour declaratively and subscribe to new events generated by SQL result sets in a reactive fashion. Being able to deal with SQL result sets in a declarative manner is an interesting property because SQL itself is a declaratively language. Perhaps, using a declarative paradigm for processing SQL result can even help with alleviating the traditional impedance mismatch between relational and object-oriented data structures.

R2DBC currently provides database drivers for PostGreSQL, Microsoft SQL Server and the H2 in-memory database

Leave a Comment

*

I agree