This is a read-only copy of the MariaDB Knowledgebase generated on 2025-02-22. For the latest, interactive version please visit https://mariadb.com/kb/.

Connect with MariaDB Connector/R2DBC (Native API)

Overview

Java developers can use MariaDB Connector/R2DBC to establish client connections with MariaDB database products.

Code Example: Connect

Connections are created, used, and managed using the following Java classes:

ClassDescription
org.mariadb.r2dbc.MariadbConnectionFactoryCreates client connections.
org.mariadb.r2dbc.MariadbConnectionConfigurationConfigures client connections for the connection factory.
io.r2dbc.spi.ConnectionImplements the R2DBC client connection.

The following code example connects to a server using the database and user account created in Setup for Examples:

// Module Imports
import org.mariadb.r2dbc.MariadbConnectionConfiguration;
import org.mariadb.r2dbc.MariadbConnectionFactory;
import io.r2dbc.spi.Connection;

// Main Application Class
public class App {
   // Connection Configuration
   private static MariadbConnectionConfiguration conf;
   private static MariadbConnectionFactory connFactory;
   private static Connection conn;

   // Main Process
   public static void main(String[] args) {
      //Initialize Connection Factory
      initConnectionFactory();

     //Initialize a Connection
     conn = connFactory.create().block();

      // Use the connection
      //conn.
   }

   public static void initConnectionFactory() {
      try {
         // Configure the Connection
         conf = MariadbConnectionConfiguration.builder()
              .host("192.0.2.1").port(3306)
              .username("db_user").password("db_user_password")
              .database("test").build();

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

      }
      catch (java.lang.IllegalArgumentException e) {
         System.err.println("Issue encountered while getting connection");
         e.printStackTrace();
      }
   }
}
  • The connection must be configured for either host/port or socket, but it cannot be configured for both host/port and socket.
  • For maximum portability, connections should be used synchronously.
  • Objects created by a connection are only valid as long as the connection remains open.
  • When configuring a connection, R2DBC applications should use the appropriate methods such as beginTransaction(), setAutoCommit(boolean), and setTransactionIsolationLevel(IsolationLevel) to change transaction properties. Applications should not execute SQL commands directly to change the connection configuration when a R2DBC method is available.
  • New connections are by default created in auto-commit mode.
  • When you are done with a connection, close it to free resources. Close the connection using the close() method.
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.