Using the rxjava2-jdbc library

David Moten's rxjava2-jdbc library (https://github.com/davidmoten/rxjava2-jdbc) was created to wrap a JDBC driver in a way that does not block a reactive application. The library is built on RxJava 2 and uses a dedicated thread pool and the concept of the non-blocking connection pool. Consequently, a request does not block a thread while waiting for a free connection. As soon as a connection is available, the query starts executing on the connection and blocks the thread. The application may not manage a dedicated scheduler for blocking requests because the library does this. Additionally, the library has a fluent DSL that allows us to issue SQL statements and receive results as Reactive Streams. Let's define the Book entity and annotate it properly for use with rxjava2-jdbc:

@Query("select id, title, publishing_year " +                        // (3)
       "from book order by publishing_year")
public interface Book {                                              // (1)
   @Column String id();                                              // (2)
   @Column String title();
   @Column Integer publishing_year();
}

In the preceding code, we can see the following:

  1. We define the Book interface. Note that with Spring Data, we usually define an entity as a class.
  2. Accessor methods are decorated with the @Column annotation. The annotation helps to map row columns to entity fields.
  3. With the @Query annotation, we define the SQL statement used for the entity retrieval.

Now let's define a simple repository that finds books published during a certain period:

public class RxBookRepository {
    private static final String SELECT_BY_YEAR_BETWEEN =           // (1)
         "select * from book where " +
         "publishing_year >= :from and publishing_year <= :to";

    private final String url = "jdbc:h2:mem:db";
    private final int poolSize = 25;
    private final Database database = Database.from(url, poolSize);// (2)

    public Flowable<Book> findByYearBetween(                       // (3)
      Single<Integer> from,
      Single<Integer> to
   ) {
      return Single
         .zip(from, to, Tuple2::new)                               // (3.1)
         .flatMapPublisher(tuple -> database                       //
            .select(SELECT_BY_YEAR_BETWEEN)                        // (3.2)
            .parameter("from", tuple._1())                         // (3.3)
            .parameter("to", tuple._2())                           //
            .autoMap(Book.class));                                 // (3.5)
   }
}

Let's describe the implementation of the RxBookRepository class as follows:

  1. As the library cannot generate queries automatically, we have to provide the SQL query that searches required books. Named parameters are allowed in an SQL query.
  2. Database initialization requires a JDBC URL and the pool size. In our case, no more than 25 concurrent queries may run at the same time.
  1. The findByYearBetween method uses reactive types from the RxJava 2 library (Flowable and Single), not from Project Reactor. This is because the rxjava2-jdbc library internally uses RxJava 2.x and exposes RxJava types through its API. However, it is easy to convert RxJava types to types from Project Reactor. At point (3.1) we subscribe to the stream that resolves request arguments. Then we call the select method (3.2) and fill the query parameters (3.3). The autoMap method translates a JDBC row into a Book entity. The autoMap method returns the Flowable<Book>which is equivalent to Project Reactor's Flux<Book>.

The rxjava2-jdbc library supports most JDBC drivers. Also, the library has some support for transactions. All the operations inside a transaction have to be executed on the same connection. The commit/rollback of the transaction happens automatically.

The rxjava2-jdbc library is neat, reduces some potential thread blocks, and makes it possible to work with relational databases reactively. However, so far, it is still new and may not handle complex reactive workflows, especially those that involve transactions. The rxjava2-jdbc library also needs definitions for all SQL queries.

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

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