Asynchronous Database Access

Asynchronous Database Access (ADBA) defines a non-blocking database access API for Java platform. At the time of writing, it is still a draft, and JDBC Expert Group is discussing what it should look like. ADBA was announced at the JavaOne 2016 conference and has been under discussion for a couple of years now. ADBA is intended to complement the current JDBC API and propose an asynchronous alternative (not a replacement) geared toward high-throughput programs. ADBA is designed to support the fluent programming style and provide a builder pattern for composing database queries. ADBA is not an extension to JDBC and has no dependencies on it. When ready, most likely, ADBA will live in the java.sql2 package.

ADBA it is an asynchronous API, so no method calls should block while doing a network call. All potentially blocking actions are represented as separate ADBA operations. A client application builds and submits one ADBA operation or a graph of ADBA operations. A driver that implements ADBA asynchronously executes the operation and reports the result via java.util.concurrent.CompletionStage or callbacks. When ready, an asynchronous SQL that requests issues over ADBA may look as follows:

CompletionStage<List<String>> employeeNames =                        // (1)
connection
   .<Integer>rowOperation("select * from employee")                  // (2)
   .onError(this::userDefinedErrorHandler)                           // (3)
   .collect(Collector.of(                                            // (4)
      () -> new ArrayList<String>(),
      (ArrayList<String> cont, Result.RowColumn row) ->
          cont = cont.add(row.at("name").get(String.class)),
      (l, r) -> l,
      container -> container))
   .submit()                                                         // (5)
   .getCompletionStage();                                            // (6)

Note that the preceding code is still based on an ADBA draft so that the API may change at any time. However, the numbered lines mean the following:

  1. The query returns result in CompletionStage. In our case, we return a list of employees' names.
  2. This initiates a new row operation by calling the rowOperation method of the database connection.
  3. The operation allows registering error handlers by invoking the onError method. Error handling happens in our custom userDefinedErrorHandler method.
  1. The collect method allows gathering results using Java Stream API collectors.
  2. The submit method starts processing the operation.
  3. The getCompletionStage method gives the user an instance of CompletionStage that will hold the result when the processing is finished.

Of course, ADBA is going to provide the ability to write and execute more complex database queries, including conditional, parallel, and dependent operations. ADBA has support for transactions. However, in contrast with JDBC, ADBA is not intended to be used directly by business code (even though it is possible), but rather intended to provide an asynchronous basis for more high-level libraries and frameworks.

At the time of writing, ADBA has only one implementation, called AoJ. AoJ (https://gitlab.com/asyncjdbc/asyncjdbc/tree/master) is an experimental library that implements a subset of the ADBA API by invoking the standard JDBC on a separate thread pool. AoJ is not suitable for production use, but provides the ability to play with ADBA without the need to implement a full-blown asynchronous driver.

There are some conversations regarding ADBA's ability to return results not only with CompletionStage but also with a reactive Publisher from the Java Flow API. However, it is not clear how the Flow API would be integrated into ADBA or whether ADBA would expose a reactive API at all. At the time of writing, this topic is still the subject of hot debate.

At this point, we have to claim again that asynchronous behavior represented by Java's CompletionStage may always be substituted by a reactive Publisher implementation. However, this statement is not valid for the reverse situation. Reactive behavior may be represented with CompletionStage or CompletableFuture only with some compromises, namely by dropping backpressure propagation. Also, with CompletionStage<List<T>>, there is no actual data streaming semantic as clients are required to wait for the complete result set. Leveraging the Collector API for streaming doesn't really seem to be an option here. Besides, CompletableFuture begins its execution as soon as it is submitted, while Publisher begins execution only when it receives a subscription. A reactive API for database access would fulfill the needs for both a reactive language-level API and an asynchronous language-level API. It is because any reactive API could be quickly turned to an asynchronous API without any semantic compromises. However, an asynchronous API, in most cases, may become a reactive API only with some compromises. That is why, from the standpoint of the authors of this book, ADBA with Reactive Streams support seems to be more beneficial than asynchronous-only ADBA.

The alternative candidate for the next-generation data-access API in Java is called R2DBC. It has more to offer than the entirely asynchronous ADBA and proves that a reactive API for relational data access has huge potential. So, let's look at it more closely.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.124.177