Getting Started with Reactive Relational Database Connectivity and the Oracle R2DBC Driver
An Introduction to R2DBC — Reactive Relational DB Connectivity
R2DBC stands for Reactive Relational Database Connectivity, and it defines a set of reactive APIs for database access in Java. It is an open specification for asynchronous, non-blocking communication with relational databases, and it defines a Service Provider Interface (SPI) for driver vendors to implement and clients to consume.
R2DBC adopts a reactive programming model for interacting with databases, allowing developers to work with streams of data rather than traditional blocking calls, so it aligns with the Reactive Streams specification.
R2DBC helps to improve the performance and scalability of database-driven applications by means of APIs that are asynchronous, functional, and non-blocking, and provide mechanisms to support back-pressure-aware database access.
To make it complete, below are listed the typical features supported by the reactive programming model:
- Non-blocking I/O
- Deferred execution
Treating application control as a series of events (data, errors, completion, and so on) - No longer assuming control of resources but leaving resource scheduling to the runtime or platform (“React to resource availability”)
- Efficiently using resources
- Leaving flow control to be handled by the runtime
- Stream-oriented data consumption
- Functional programming within operators
- Removing assumptions over concurrency from the programming model and leaving this aspect up to the runtime
- Using back-pressure to allow flow control, deferring the actual execution, and not overwhelming consumers
The Oracle R2DBC Driver
The Oracle R2DBC Driver is available as an Open-Source project on GitHub, and it has a dependency on the 21c or later versions of Oracle JDBC for its async APIs. Introduced in 2021 (v0.1.0), the Oracle R2DBC is a concrete implementation of the R2DBC Service Provider Interface (SPI) for the Oracle Database.
The current release comprises the additional features listed below.
- Oracle R2DBC Driver v1.2.0 implements version 1.0.0.RELEASE of the R2DBC SPI:
1. Added support for pipelined database calls
2. Added support for providing option values with a Supplier or Publisher - Oracle R2DBC Driver v1.1.1 implements version 1.0.0.RELEASE of the R2DBC SPI:
1. Allow Oracle Net Encryption and Checksum Properties
2. Added an option to configure oracle.jdbc.timezoneAsRegion
3. Added support for LDAP URLs
4. Added support for REF CURSOR values
5. Added support for user-defined ARRAY and OBJECT types
So, as an implementation of Reactive Streams, the R2DBC APIs express database operations as the Reactive Streams know standard types, a Publisher and a Subscriber.
Such types allow R2DBC to interoperate with other Reactive Stream compatible libraries, such as Project Reactor, RxJava, Akka Streams, Vert.x and others.
If you want to give it a try and work a little bit with the Oracle R2DBC Driver, use the SQL script provided in our code sample to create the target database table and insert a few records.
Below is a code sample of using R2DBC with Project Reactor to retrieve a list of users from an Oracle Database with a simple SQL query by using both Mono and Flux.
Wrapping it up
You learned about the Oracle R2DBC Driver, released as an Open-source component on GitHub.
It allows you to implement database access asynchronously in alignment with the Reactive Streams specification. Besides, you can interoperate with other Reactive Streams compatible libraries, such as Project Reactor, RxJava, Akka Streams, and Vert.x.
That’s it! I hope you enjoyed this blog post, so stay tuned!
References
Oracle Database 23c Free — Developer Release
Oracle R2DBC v1.2.0 — GitHub (Open-source)
Develop Java applications with Oracle Database
Oracle Developers and Oracle OCI Free Tier
Join our Oracle Developers channel on Slack to discuss Java, JDK, JDBC, GraalVM, Microservices with Spring Boot, Helidon, Quarkus, Micronaut, Reactive Streams, Cloud, DevOps, IaC, and other topics!
Build, test, and deploy your applications on Oracle Cloud — for free! Get access to OCI Cloud Free Tier!