R2DBC Driver for Microsoft SQL Server using TDS (Tabular Data Stream) Protocol
R2DBC Driver for Microsoft SQL Server using TDS (Tabular Data Stream) Protocol
This project contains the Microsoft SQL Server implementation of the R2DBC SPI. This implementation is not intended to be used directly, but rather to be used as the backing implementation for a humane client library to delegate to
This driver provides the following features:
TEXT, VARCHAR(MAX), IMAGE, VARBINARY(MAX) and national variants, see below for exceptions)Next steps:
This project is governed by the R2DBC Code of Conduct. By participating, you are expected to uphold this code of conduct. Please report unacceptable behavior to [email protected].
Here is a quick teaser of how to use R2DBC MSSQL in Java:
URL Connection Factory Discovery
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:mssql://:1433/");
Publisher connectionPublisher = connectionFactory.create();When connecting through a tunnel or proxy, serverName can specify the logical SQL Server name independently from the physical connection endpoint:
ConnectionFactory connectionFactory = ConnectionFactories.get(
"r2dbc:mssql://localhost:15433/?serverName=server.database.windows.net");Programmatic Connection Factory Discovery
…Supported ConnectionFactory Discovery Options
| Option | Description |
|---|---|
ssl |
Whether to use transport-level encryption for the entire SQL server traffic. |
driver |
Must be sqlserver. |
host |
Server hostname to connect to. |
port |
Server port to connect to. Defaults to 1433. (Optional) |
username |
Login username. |
password |
Login password. |
database |
Initial database to select. Defaults to SQL Server user profile settings. (Optional) |
applicationName |
Name of the application. Defaults to driver name and version. (Optional) |
connectionId |
Connection Id for tracing purposes. Defaults to a random Id. (Optional) |
connectionProvider |
Set the reactor.netty.resources.ConnectionProvider to be used when creating the connection. Defaults to ConnectionProvider.newConnection(). (Optional) |
connectTimeout |
Connection Id for tracing purposes. Defaults to 30 seconds. (Optional) |
hostNameInCertificate |
Expected hostname in SSL certificate. Supports wildcards (e.g. *.database.windows.net). (Optional) |
lockWaitTimeout |
Lock wait timeout using SET LOCK_TIMEOUT …. (Optional) |
preferCursoredExecution |
Whether to prefer cursors or direct execution for queries. Uses by default direct. Cursors require more round-trips but are more backpressure-friendly. Defaults to direct execution. Can be boolean or a Predicate accepting the SQL query. (Optional) |
sendStringParametersAsUnicode |
Configure whether to send character data as unicode (NVARCHAR, NCHAR, NTEXT) or whether to use the database encoding, defaults to true. If disabled, CharSequence data is sent using the database-specific collation such as ASCII/MBCS instead of Unicode. |
serverName |
Logical SQL Server name used as TLS SNI hostname and in the TDS login. Useful when host is a tunnel or proxy endpoint. Defaults to host, also the default for hostNameInCertificate. (Optional) |
sslTunnel |
Enables SSL tunnel usage when using a SSL tunnel or SSL terminator in front of SQL Server. Accepts Function to customize the SSL tunnel settings. SSL tunneling is not related to SQL Server's built-in SSL support. (Optional) |
sslContextBuilderCustomizer |
SSL Context customizer to configure SQL Server's built-in SSL support (Function) (Optional) |
tcpKeepAlive |
Enable/disable TCP KeepAlive. Disabled by default. (Optional) |
tcpNoDelay |
Enable/disable TCP NoDelay. Enabled by default. (Optional) |
trustServerCertificate |
Fully trust the server certificate bypassing X.509 certificate validation. Disabled by default. (Optional) |
trustStoreType |
Type of the TrustStore. Defaults to KeyStore.getDefaultType(). (Optional) |
trustStore |
Path to the certificate TrustStore file. (Optional) |
trustStorePassword |
Password used to check the integrity of the TrustStore data. (Optional) |
Programmatic Configuration
MssqlConnectionConfiguration configuration = MssqlConnectionConfiguration.builder()
.host("…")
.serverName("…") // optional, defaults to host
.username("…")
.password("…")
.database("…")
.preferCursoredExecution(…)
.build();
MssqlConnectionFactory factory = new MssqlConnectionFactory(configuration);
Mono connectionMono = factory.create();Microsoft SQL Server uses named parameters that are prefixed with @. The following SQL statement makes use of parameters:
INSERT INTO person (id, first_name, last_name) VALUES(@id, @firstname, @lastname)Parameters are referenced without the @ prefix when binding these:
connection.createStatement("INSERT INTO person (id, first_name, last_name) VALUES(@id, @firstname, @lastname)")
.bind("id", 1)
.bind("firstname", "Walter")
.bind("lastname", "White")
.execute()Binding also allows positional index (zero-based) references. The parameter index is derived from the parameter discovery order when parsing the query.
Artifacts can be found on Maven Central.
io.r2dbc
r2dbc-mssql
${version}
If you'd rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.
io.r2dbc
r2dbc-mssql
${version}.BUILD-SNAPSHOT
central-portal-snapshots
Central Portal Snapshots
https://central.sonatype.com/repository/maven-snapshots/
SQL Server supports additional options when starting a transaction. In particular, the following options can be specified:
isolationLevel) (reset after the transaction to previous value)name)mark)lockWaitTimeout) (reset after the transaction to -1)These options can be specified upon transaction begin to start the transaction and apply options in a single command round
No open issues yet, or sync has not completed.