# Connect with MariaDB Connector/R2DBC (Native API)

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:

| Class                                            | Description                                               |
| ------------------------------------------------ | --------------------------------------------------------- |
| org.mariadb.r2dbc.MariadbConnectionFactory       | Creates client connections.                               |
| org.mariadb.r2dbc.MariadbConnectionConfiguration | Configures client connections for the connection factory. |
| io.r2dbc.spi.Connection                          | Implements the R2DBC client connection.                   |

The following code example connects to a server using the database and user account created in [Setup for Examples](/docs/connectors/mariadb-connector-r2dbc/using-the-native-r2dbc-api-of-mariadb-connector-r2dbc/setup-for-connector-r2dbc-examples-native-api.md):

```java
// 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.

<sub>*This page is: Copyright © 2025 MariaDB. All rights reserved.*</sub>

{% @marketo/form formId="4316" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/connectors/mariadb-connector-r2dbc/using-the-native-r2dbc-api-of-mariadb-connector-r2dbc/connect-with-mariadb-connector-r2dbc-native-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
