Connect with MariaDB Connector/R2DBC (Spring)
This page is part of MariaDB's Documentation.
The parent of this page is: Using Spring Data with MariaDB Connector/R2DBC
Topics on this page:
Overview
Java developers can use MariaDB Connector/R2DBC to establish client connections with MariaDB database products.
Creating a Database Client
Connections are created, used, and managed using several classes:
Class | Description |
---|---|
| Creates client connections. |
| Configures client connections for the connection factory. |
| Implements the R2DBC client connection. |
| Creates a higher level, reactive client for Reactive Streams. |
Code Example: Connect
The following example shows how to use the DatabaseClient
class to connect and execute queries:
// 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 {
// 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);
// Instantiate a Database Client
client = DatabaseClient.create(connFactory);
// Create a Database Table
client.execute("CREATE TABLE IF NOT EXISTS test.contact" + "(id INT PRIMARY KEY AUTO_INCREMENT,"
+ "first_name VARCHAR(25)," + "last_name VARCHAR(25)," + "email VARCHAR(25)) ENGINE=InnoDB")
.fetch()
.rowsUpdated()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (io.r2dbc.spi.R2dbcNonTransientResourceException e) {
e.printStackTrace();
} finally {
}
}
}
After running the application App
, verify that the table has been created:
SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| contact |
+----------------+
DESCRIBE contact;
+------------+-------------+------+-----+--------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+----------+---------------+
| id | int(11) | NO | PRI | NULL | auto_increment|
| first_Name | varchar(25) | YES | | NULL | |
| last_Name | varchar(25) | YES | | NULL | |
| email | varchar(25) | YES | | NULL | |
+------------+-------------+------+-----+----------+---------------+
Connection Pools
A DatabaseClient
uses the underlying ConnectionFactory
to get and release connections for each database operation without affinity to a particular connection across the multiple operations. When using Spring's R2DBC layer, a custom connection pool could be configured using an implementation provided by a third party.