Introduction to JDBC Reactive Extensions with the Oracle Database 23c Free — Developer Release
Introduction
JDBC Reactive Extensions are a set of asynchronous APIs that extend the standard JDBC interfaces to enable non-blocking database access using Reactive Streams.
Such Java Reactive Streams API provides a JVM-standardized way to write asynchronous code that can handle backpressure and perform efficiently on multiple threads.
Now, why should you adopt the JDBC Reactive Extensions? Easy, they provide several benefits over the classic blocking JDBC APIs as listed below.
Improved scalability, performance and resource utilization
- Improved scalability: Asynchronous operations can use multiple threads simultaneously, significantly improving database application performance.
- Reduced resource consumption: Asynchronous operations do not block threads, which can reduce an application’s overall resource utilization (CPU, memory, disk, network bandwidth, etc).
- Improved responsiveness: The fire-and-forget approach of asynchronous operations allows applications to send requests and remain responsive while database operations are in progress.
No additional JARs are needed
The JDBC Reactive Extensions are available in the Oracle JDBC Thin Driver 21c or later. The extensions expose standard Java Flow Subscriber and Publisher types and APIs that can interoperate with the R2DBC API and other Reactive Streams libraries.
Such libraries include Project Reactor, RxJava, Akka, Vert.x, Spring, jOOQ, Querydsl, Kotysa and so on which furnish Reactive Streams operators (map, reduce, filters), concurrency modeling, monitoring, and tracing.
Interestingly, if you are using the Oracle JDBC Driver, you do not have to include another JAR as a dependency. The JDBC Reactive Extensions belong to the same dependency artefact (JAR file) as the Oracle JDBC Driver.
The block diagram below shows a red dashed rectangle with both software components.
To use the Reactive Extensions, you must also use JDK 11 or later.
No need to worry about the low-level mechanics of the Flow API
Another great benefit is that you do not have to worry about the low-level mechanics of working with the Flow API, as everything is handled transparently for you.
The java.util.concurrent.Flow
types define the minimal set of operations that you can use to create a reactive stream.
You can write application code directly against the Flow API. However, you must implement the low-level signal processing in accordance with the reactive-streams specification as specified in the following link
https://github.com/reactive-streams/reactive-streams-jvm/blob/master/README.md
The JDBC Reactive Extensions APIs handle the low-level mechanics of the Flow API to the application using java.util.concurrent.Flow.Publisher
and java.util.concurrent.Flow.Subscriber
. Popular libraries such as Reactor, RxJava, and Akka-Streams interface with the org.reactivestreams.Publisher
and org.reactivestreams.Subscriber
types defined by the reactive-streams-jvm
project as specified in the following link:
Although the org.reactivestreams.Publisher
type and the org.reactivestreams.Subscriber
type, and their java.util.concurrent.Flow
counterparts declare the same interface, the Java compiler must still consider them to be distinct types. You can convert between the Flow type and the org.reactivestreams
type using the org.reactivestreams.FlowAdapters
class as specified in the following link
Oracle recommends that you use the Reactive Streams libraries when interfacing with the Flow types exposed by the JDBC driver.
Execution of SQL statements with asynchronous methods
This section describes how to execute SQL statements with asynchronous methods. The OraclePreparedStatement
interface exposes methods for asynchronous SQL execution.
Each asynchronous method performs a function that is analogous to the corresponding synchronous method of SQL execution. This relationship is expressed in the following table:
Executing an async SQL DDL statement with JDBC Reactive Extensions
After this quick introduction to the rationale behind using JDBC Reactive Extensions and the general value proposition, it’s time to start coding.
Let’s look at the different database access scenarios now!
The following is an example of how to use the Reactive Extensions to execute a DDL SQL statement that will create a database table asynchronously.
After executing it, you can connect to a running database instance such as Oracle Database 23c Free — Developer Release and then just run a DESCRIBE command to check the details of your new database table as below.
In a similar fashion, you can run asynchronous SQL queries, batch updates and all else as usually required.
The following sections provide more information about the asynchronous methods:
- Standard SQL Statement Execution with the executeAsyncOracle Method
- DML Statement Execution with the executeUpdateAsyncOracle method
- Batch DML Statement Execution with the executeBatchAsyncOracle Method
- SQL Query Execution with the executeQueryAsyncOracle Method
Limitations of JDBC Reactive Extensions
This section describes the limitations of JDBC Reactive Extensions.
JDBC Reactive Extensions have the following limitations:
- You must access the asynchronous methods through the
java.sql.Wrapper.unwrap
method, instead of accessing those through a typecast. This ensures correct behaviour of the asynchronous methods when you use them with proxy Oracle JDBC classes, for example, when you use asynchronous methods with connection pools. - Reading large responses from the network may require blocking I/O bound operations. Blocking read operations can occur if the driver reads a response that is larger than the TCP Receive buffer size.
- Asynchronous SQL execution supports neither scrollable
ResultSet
types nor sensitiveResultSet
types.
Wrapping it up
That’s it! You learned about the JDBC Reactive Extensions as a powerful set of asynchronous APIs that extend the standard JDBC interfaces to enable non-blocking database access using Reactive Streams.
I hope you enjoyed this blog post, so stay tuned!
References
Oracle Database 23c Free — Developer Release
JDBC Reactive Extensions — Oracle JDBC 23c
JDK 21 Javadocs — java.util.concurrent.Flow
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!