Data Management Global

Data Management Global

A hub for collaboration, learning, networking, and cultural exchange, and contributing to positive global engagement

 View Only

Announcing the availability of IBM Db2 Java Reactive Driver

By Rajendran Appavu posted Thu April 27, 2023 01:03 AM

  

If you work on Java Reactive Applications and use Project Reactor framework, you will require a database driver that provides a non-blocking asynchronous API to access the database. As conventional JDBC drivers use blocking synchronous API by design, we need a new driver that can provide non-blocking asynchronous API. The new IBM Db2 Java Reactive Driver fills that gap. It provides non-blocking asynchronous API based on R2DBC.

We are happy to announce the release of our all-new IBM Db2 Java Reactive Driver. This driver provides a true non-blocking asynchronous access to Db2 on Z and LUW.  This product can be downloaded from Maven, add the following dependency to your pom file.

 <dependency>

     <groupId>com.ibm.db2</groupId>

     <artifactId>db2-r2dbc</artifactId>

     <version>1.1.0</version>

 </dependency>

Architecture of this driver is shown below.

IBM Db2 Java Reactive Driver Architecture

The green colored components are part of the Java Reactive Driver. The gray colored components are the third-party frameworks used by the driver.

Following code sample shows how connection configuration can be built.

DB2ConnectionConfiguration config = DB2ConnectionConfiguration.builder()

                          .database(“mydb”)

                          .host(“myhost”)

                          .port(1234)

                          .username(“ts1234”)

                          .password(“mypassword”)

                          .conPoolSize(100)

                          .stmtCacheSize(30)

                          .build();

From the connection configuration, connection factory can be built as shown below.

      DB2ConnectionFactory factory = new DB2ConnectionFactory(config);

From connection factory, a connection pool can be created as shown below:

DB2ConnectionPool pool = new DB2ConnectionPool(factory);

From the connection pool, a database connection can be obtained and a DDL can be executed, all asynchronously as shown by the following code sample to create a DEMO table.

AtomicReference<DB2Connection> con = new AtomicReference<DB2Connection>();

            

       Mono<Void> mono = pool.getConnection()

                    .doOnNext(c -> con.set(c))

                    .flatMap(c -> c.executeUpdate(

                                 "CREATE TABLE DEMO (" +

                                       "ID INTEGER UNIQUE NOT NULL, " +

                                       "ACC_BAL DECIMAL(10,2) NOT NULL, " +

                                       "AS_OF TIMESTAMP NOT NULL" +

                                 ")"

                    ))

                    .doOnTerminate(() -> con.get().release());

You can execute a DML statement to insert data into the newly created table asynchronously as shown below.

       Mono<DB2Result> mono = pool.getConnection()

             .doOnNext(c -> con.set(c))

             .flatMap(c -> c.createStatement("INSERT INTO DEMO (ID, ACC_BAL,
                                 AS_OF) VALUES (?, ?, ?)"
)

                                        .bind(1, id)

                                        .bind(2, amount)

                                        .bind(3, Timestamp.from(Instant.now()))

                                        .execute())

             .doAfterTerminate(() -> con.get().release());

You can execute SQL queries asynchronously as shown below.

       // Query the inserted data and then release the connection

       Flux<Row> flux = pool.getConnection()

             .doOnNext(c -> con.set(c))

             .flatMap(c -> c.createStatement("SELECT ID, ACC_BAL, AS_OF FROM “+

                                              “DEMO WHERE ID = ?")

                 .bind(1, id)

                 .execute())

                 .flatMapMany(result -> result.map((row, md) -> row))

                 .doOnNext(row ->

                     System.out.println ("ID: "+row.get("ID")+", ACC_BAL: "

                        +row.get("ACC_BAL")+", AS_OF: "+row.get("AS_OF")))

                 .doOnError(e ->

                     System.out.println ("Error querying account details - "+e))

                 .doAfterTerminate(() -> con.get().release());

Once done, all the database connections in the connection pool can be closed as shown below.

// close all the connections in the pool

Mono<Void> mono = pool.closeAll();

With the above code samples, we tried to give a glimpse of the new reactive driver we have for you.

For more details on the IBM Db2 Java Reactive Driver please refer to the GitHub link given below:

https://github.com/ibmdb/java_reactive_driver

The above home page contains Installation Guide, Developer Guide, Debugging Guide, Code Samples, and the Test Cases for the Java Reactive Driver.

0 comments
12 views

Permalink