Connect with R2DBC
On this page
R2DBC (Reactive Relational Database Connectivity) is a specification and set of APIs for connecting to relational databases from Java and other JVM languages.
Driver Compatibility
-
Java 8+ JDK
-
SingleStore version 9.
0+
Install the SingleStore R2DBC Connector
To install the connector using Maven, include the following dependency in the pom. file of your project.x. with the connector version you want to use.
<dependency><groupId>com.singlestore</groupId><artifactId>r2dbc-singlestore</artifactId><version>x.x.x</version></dependency>
Note
To find the latest version of the driver, refer to the R2DBC Driver for SingleStore Maven repository.
Use the SingleStore R2DBC Connector
Create a ConnectionFactory to configure the connection to your SingleStore deployment.
-
Builder API: Use
SingleStoreConnectionConfigurationandSingleStoreConnectionFactoryfor an explicit, type-safe configuration.For example: import com.singlestore.r2dbc.SingleStoreConnectionConfiguration;import com.singlestore.r2dbc.SingleStoreConnectionFactory;import io.r2dbc.spi.ConnectionFactory;SingleStoreConnectionConfiguration conf =SingleStoreConnectionConfiguration.builder().host("<host>").port(<port>).username("<username>").password("<password>").database("<database>").build();// Implementation of io.r2dbc.spi.ConnectionFactoryConnectionFactory factory = new SingleStoreConnectionFactory(conf); -
Connection URL: Use the standard SPI (Service Provider Interface) factory lookup and a R2DBC URL.
The connection string must have the following format: r2dbc:singlestore:[sequential:|loadbalancing:]//<username>:<password>@<host>:<port>/[database][?connection_options=value] For example:
import io.r2dbc.spi.ConnectionFactories;import io.r2dbc.spi.ConnectionFactory;ConnectionFactory factory = ConnectionFactories.get("r2dbc:singlestore://s2user:p455w04d@svchost:3306/dbTest");
Refer to Connection Options for the options supported in the connection configuration.
Failover and Load-Balancing Modes
Failover occurs when the connection to the primary host becomes unavailable and the connector tries reconnecting to another host.
Upon failover, the connector attempts the following:
-
Connection recovery: The connector tries reconnecting to another host in the host list.
-
Re-run the command (transaction): After a connection is re-established successfully, the connector attempts to re-run the affected query.
After a successful reconnection: -
If the connector can replay the query successfully, the query is run without returning any connectivity-related errors.
-
If the connector is unable to transparently handle the failure scenario, it returns an error.
-
The connector supports the following load-balancing modes:
-
sequential -
loadbalancing
Load balancing enables the connector to distribute the load across multiple hosts.
Refer to the SingleStore R2DBC Connector GitHub repository for more information.
Connection Options
|
Option |
Description |
Default Value |
|---|---|---|
|
|
Comma-separated list of endpoints or IP addresses of the SingleStore deployment. This option is non-operational if the | |
|
|
Port of the SingleStore deployment. |
|
|
|
Username of the SingleStore database user with which to connect. | |
|
|
Password for the SingleStore database user. | |
|
|
Name of the SingleStore database to connect with. | |
|
|
Specifies the connection timeout value (in seconds). |
|
|
|
Specifies whether the underlying network connection uses TCP keepalive probes. |
|
|
|
If enabled, resets TCP connections using an abortive (or hard) close instead of an orderly (graceful) close. This option is useful in environments where connections are opened and closed in rapid succession, where new sockets may eventually fail to be created because all local ephemeral ports are consumed by TCP connections in the |
|
|
|
Enables the use of a Unix domain socket for faster database connections where the database is locally deployed. | |
|
|
Enables the connector to submit multiple queries in a single call. Because it allows for SQL injection, this may pose a security risk. |
|
|
|
Specifies additional client information to send to the database. Query the MV_ | |
|
|
Specifies the session variables to set upon a successful connection. | |
|
|
Force enables TLS/SSL protocol for the connection, specified as a comma-separated list. |
Java default |
|
|
Use this option to specify the server's certificate in DER format or specify the server's CA certificate. You can specify the server certificate in one of the following formats:
| |
|
|
Use this option to specify the client's certificate in DER format. You can specify the client certificate in one of the following formats:
| |
|
|
The client's private key path for mutual authentication. | |
|
|
Password of the client private key. | |
|
|
Specifies the SSL/TLS mode.
|
|
|
|
Enables customization of SSL context builder. | |
|
|
Disables hostname verification during SSLHandshake when | |
|
|
Enables using prepared statements. |
|
|
|
Specifies the prepared statement cache size (number of saved statements). If |
|
|
|
Specifies an additional password for PAM authentication in multi-step authentication. | |
|
|
Sets the | |
|
|
Enables sharing a Netty | |
|
|
Specifies whether initialization commands sent after a connection is established are skipped. The connector expects the server to meet the following conditions:
| |
|
|
Enables extended data types, by enabling the |
Example
The following example connects to a SingleStore deployment, inserts data into a table, and then queries the data.
-
Create a Maven project.
mvn archetype:generate \-DgroupId=com.example \-DartifactId=singlestore-r2dbc-demo \-DarchetypeArtifactId=maven-archetype-quickstart \-DinteractiveMode=false -
Add the required dependencies and plugins to the
pom.file of the project.xml This example includes the following: <dependencies><dependency><groupId>com.singlestore</groupId><artifactId>r2dbc-singlestore</artifactId><version>1.0.0</version></dependency><!-- Reactor, used by R2DBC APIs --><dependency><groupId>io.projectreactor</groupId><artifactId>reactor-core</artifactId><version>3.6.5</version></dependency><!-- Simple logging (optional but helpful) --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.9</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><plugins><!-- Compiler plugin (already controlled by properties) --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version></plugin><!-- To run the app via `mvn exec:java` --><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>3.1.0</version><configuration><mainClass>com.example.R2dbcExample</mainClass></configuration></plugin></plugins></build> -
Create a main class.
This example builds a SingleStoreConnectionConfigurationandSingleStoreConnectionFactoryusing the connector’s builder API.package com.example;import com.singlestore.r2dbc.SingleStoreConnectionConfiguration;import com.singlestore.r2dbc.SingleStoreConnectionFactory;import io.r2dbc.spi.Connection;import io.r2dbc.spi.ConnectionFactory;import reactor.core.publisher.Flux;import reactor.core.publisher.Mono;public class R2dbcExample {public static void main(String[] args) {// 1) Configure connection (update these with your actual values)SingleStoreConnectionConfiguration conf =SingleStoreConnectionConfiguration.builder().host("svchost").port(3306).username("s2user").password("passkey").database("dbTest").build(); // builder style// 2) Create a ConnectionFactoryConnectionFactory factory = new SingleStoreConnectionFactory(conf);// 3) Use R2DBC reactively: create table, insert, and queryMono.from(factory.create()).flatMapMany(connection ->// use a helper that ensures the connection is closedrunExampleQueries(connection).doFinally(signal -> Mono.from(connection.close()).subscribe())).doOnNext(rowString -> System.out.println("Row: " + rowString)).blockLast(); // block to keep the JVM alive for this simple demo}private static Flux<String> runExampleQueries(Connection connection) {// Simple schema + dataString createTableSql ="CREATE TABLE IF NOT EXISTS demo_r2dbc (" +" id INT PRIMARY KEY, " +" name VARCHAR(64)" +")";String insertSql ="INSERT INTO demo_r2dbc (id, name) VALUES (1, 'Alice'), (2, 'John'), (3, 'Jane') " +"ON DUPLICATE KEY UPDATE name = VALUES(name)";String selectSql = "SELECT id, name FROM demo_r2dbc";// Execute steps in sequence:return Mono.from(connection.createStatement(createTableSql).execute()).flatMapMany(result -> result.getRowsUpdated()).thenMany(Mono.from(connection.createStatement(insertSql).execute()).flatMapMany(r -> r.getRowsUpdated())).thenMany(Mono.from(connection.createStatement(selectSql).execute()).flatMapMany(result ->result.map((row, meta) -> {Integer id = row.get("id", Integer.class);String name = row.get("name", String.class);return "id=" + id + ", name=" + name;})));}}Alternatively, you can construct the
ConnectionFactorywith a R2DBC URL instead of the builder.For example: ConnectionFactory factory =ConnectionFactories.get("r2dbc:singlestore://s2user:password@svchost:3306/dbtest"); -
Build and run the application.
Run the following command from the project root directory. mvn -q clean compile exec:javaRow: id=1, name=Alice Row: id=2, name=John Row: id=3, name=JaneA similar output indicates that the application ran as expected.
Last modified: January 30, 2026