MariaDB Connector/R2DBC

Java developers can use MariaDB Connector/R2DBC to connect to MariaDB Platform using the Reactive Relational Database Connectivity (R2DBC) API. R2DBC operations are non-blocking, which makes the R2DBC API more scalable than Java's standard JDBC API. It is available both with a native R2DBC implementation and the Spring Data R2DBC framework

Overview

Connector

MariaDB Connector/R2DBC

Supported Versions

0.8

Programming Language

Java

Programming Language Version

Java 8+

API

R2DBC 0.8.2

Supports TLS

Yes

Supports Connection Pools

Yes

License

Apache 2.0

Note

MariaDB Connector/R2DBC 0.8.4 is a release candidate (RC) release. It is not recommended for production usage.

Benefits of R2DBC

The R2DBC API relies on reactive data streams, which provide many benefits over standard JDBC:

  • Reactive: R2DBC uses an event-driven, non-blocking, and functional programming model.

  • Scalable: R2DBC is more scalable than standard JDBC, because it allows the runtime to handle concurrency and scheduling.

  • Fast: R2DBC streams results, which can improve performance.

  • Efficient: R2DBC uses deferred execution and flow control to ensure that operations are performed efficiently.

  • Simple: R2DBC is designed to make common usage patterns simple to implement for developers.

Usage

For details on how to use MariaDB Connector/R2DBC, choose a supported framework:

Native R2DBC

The native implementation of R2DBC can be used to connect using MariaDB Connector/R2DBC from within your Java application.

Spring Data R2DBC

Spring Data implementation of R2DBC allows you to connect using MariaDB Connector/R2DBC using the Spring Framework.

Feature

Native R2DBC

Spring Data R2DBC

Reactive

Yes

Yes

Scalable

Yes

Yes

Fast

Yes

Yes

Efficient

Yes

Yes

Executes SQL

Yes

Yes

Integrates with Spring Framework

No

Yes

Spring Data: DatabaseClient

No

Yes

Spring Data: Repositories

No

Yes

Spring Data: Object Mapping

No

Yes

Spring Data: Transaction Management

No

Yes

Examples

Native R2DBC

The following example uses the native R2DBC API to select data from the table defined in Setting up the Environment:

//Module Imports
import org.mariadb.r2dbc.MariadbConnectionConfiguration;
import org.mariadb.r2dbc.MariadbConnectionFactory;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.Statement;
import io.r2dbc.spi.Result;
import io.r2dbc.spi.Row;
import io.r2dbc.spi.RowMetadata;
import reactor.core.publisher.Flux;

public class App {

   // Connection Configuration
   private static MariadbConnectionConfiguration conf;
   private static MariadbConnectionFactory connFactory;
   private static Connection conn;

   public static void main(String[] argv) {

      // Initialize Connection
      initConnection();

      // Print contacts
      printContactList();

      // Close Connection
      conn.close();
   }

   public static void initConnection() {

      try {
         // Configure the Connection
         conf = MariadbConnectionConfiguration.builder()
            .host("192.0.2.1")
            .port(3306)
            .username("connr2dbc_test")
            .password("passwd")
            .database("test")
            .build();

         // Instantiate a Connection Factory
         connFactory = new MariadbConnectionFactory(conf);

         // Instantiate a Connection
         conn = connFactory.create().block();
      }
      catch (java.lang.IllegalArgumentException e) {
         System.err.println("Issue encountered while getting connection");
         e.printStackTrace();
      }
   }

   public static void printContactList() {

      try {
         // Initialize Statement
         Statement stmnt = conn.createStatement(
            "SELECT first_name, last_name, email FROM test.contacts");

         // Execute Statement and Iterate over the Result-set
         for (String contact_entry : Flux.from(stmnt.execute()).flatMap( res ->
            res.map( (row, metadata) -> {

            return String.format( "- %s %s <%s>",
               // Get First Name
               row.get(0, String.class),

               //  Get Last Name
               row.get(1, String.class),

               //Get Email
               row.get(2, String.class));
         })).toIterable()) {

            System.out.println(contact_entry);
         }
      }
      // Catch Exception
      catch (java.lang.IllegalArgumentException e) {
         System.err.println("Encountered issue querying contact list");
         e.printStackTrace();
      }
   }
}

Spring Data R2DBC

The following example uses the Spring Data R2DBC framework to select data from the table defined in Setting up the Environment:

//Module Imports
import org.mariadb.r2dbc.MariadbConnectionConfiguration;
import org.mariadb.r2dbc.MariadbConnectionFactory;
import org.springframework.data.r2dbc.core.DatabaseClient;
import reactor.test.StepVerifier;

// Main Application Class
public class App {

   // Connection Configuration
   private static MariadbConnectionConfiguration conf;
   private static MariadbConnectionFactory connFactory;

   private static DatabaseClient client;

   // Main Process
   public static void main(String[] args) {

      try {
         conf = MariadbConnectionConfiguration.builder()
               .host("192.0.2.1")
               .port(3306)
               .username("connr2dbc_test")
               .password("passwd")
               .database("test")
               .build();

         // Instantiate a Connection Factory
         connFactory = new MariadbConnectionFactory(conf);

         // Instantiate a Database Client
         client = DatabaseClient.create(connFactory);

         // Select all rows
         client.select()
            .from(Contact.class)
            .fetch().all()
            .doOnNext(it -> System.out.println(it))
            .as(StepVerifier::create)
            .expectNextCount(3)
            .verifyComplete();

         // Select the first row
         client.select()
            .from(Contact.class)
            .fetch().first()
            .doOnNext(it -> System.out.println(it))
            .as(StepVerifier::create)
            .expectNextCount(1)
            .verifyComplete();

         // Select all rows with explicit query
         client.execute("SELECT id, first_name,last_name,email FROM contact")
            .as(Contact.class)
            .filter(s -> s.fetchSize(25))
            .fetch().all()
            .doOnNext(it -> System.out.println(it))
            .as(StepVerifier::create)
            .expectNextCount(3)
            .verifyComplete();

         // Select single column
         client.execute("SELECT first_name FROM contact")
            .map((row, rowMetadata) -> row.get("first_name", String.class))
            .all()
            .doOnNext(it -> System.out.println(it))
            .as(StepVerifier::create)
            .expectNextCount(3)
            .verifyComplete();

      } catch (IllegalArgumentException e) {
         e.printStackTrace();
      } catch (io.r2dbc.spi.R2dbcNonTransientResourceException e) {
         e.printStackTrace();
      } finally {
      }

   }

}