Skip to content

Commit ea66940

Browse files
snicollmp911de
andcommitted
Document R2DBC support
Closes gh-19988 Co-authored-by: Mark Paluch <[email protected]>
1 parent 80bb9c5 commit ea66940

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

spring-boot-project/spring-boot-docs/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {
145145
"spring-data-jpa-version": versionConstraints["org.springframework.data:spring-data-jpa"],
146146
"spring-data-mongodb-version": versionConstraints["org.springframework.data:spring-data-mongodb"],
147147
"spring-data-neo4j-version": versionConstraints["org.springframework.data:spring-data-neo4j"],
148+
"spring-data-r2dbc-version": versionConstraints["org.springframework.data:spring-data-r2dbc"],
148149
"spring-data-rest-version": versionConstraints["org.springframework.data:spring-data-rest-core"],
149150
"spring-data-solr-version": versionConstraints["org.springframework.data:spring-data-solr"],
150151
"spring-framework-version": versionConstraints["org.springframework:spring-core"],

spring-boot-project/spring-boot-docs/src/docs/asciidoc/attributes.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
:spring-data-mongodb-api: https://docs.spring.io/spring-data/mongodb/docs/{spring-data-mongodb-version}/api/org/springframework/data/mongodb
7373
:spring-data-neo4j: https://spring.io/projects/spring-data-neo4j
7474
:spring-data-neo4j-docs: https://docs.spring.io/spring-data/neo4j/docs/{spring-data-neo4j-version}/reference/html/
75+
:spring-data-r2dbc-api: https://docs.spring.io/spring-data/r2dbc/docs/{spring-data-r2dbc-version}/api/org/springframework/data/r2dbc
76+
:spring-data-r2dbc-docs: https://docs.spring.io/spring-data/r2dbc/docs/{spring-data-r2dbc-version}/reference/html/
7577
:spring-data-redis: https://spring.io/projects/spring-data-redis
7678
:spring-data-rest-api: https://docs.spring.io/spring-data/rest/docs/{spring-data-rest-version}/api/org/springframework/data/rest
7779
:spring-data-solr: https://spring.io/projects/spring-data-solr

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3774,6 +3774,139 @@ You can also create your own `org.jooq.Configuration` `@Bean` if you want to tak
37743774

37753775

37763776

3777+
[[boot-features-r2dbc]]
3778+
=== Using R2DBC
3779+
The Reactive Relational Database Connectivity (https://r2dbc.io[R2DBC]) project brings reactive programming APIs to relational databases.
3780+
R2DBC's `io.r2dbc.spi.Connection` provides a standard method of working with non-blocking database connections.
3781+
Connections are provided via a `ConnectionFactory`, similar to a `DataSource` with jdbc.
3782+
3783+
`ConnectionFactory` configuration is controlled by external configuration properties in `+spring.r2dbc.*+`.
3784+
For example, you might declare the following section in `application.properties`:
3785+
3786+
[source,properties,indent=0]
3787+
----
3788+
spring.r2dbc.url=r2dbc:postgresql://localhost/test
3789+
spring.r2dbc.username=dbuser
3790+
spring.r2dbc.password=dbpass
3791+
----
3792+
3793+
TIP: You do not need to specify a driver class name, since Spring Boot obtains the driver from R2DBC's Connection Factory discovery.
3794+
3795+
NOTE: At least the url should be provided.
3796+
Information specified in the URL takes precedence over individual properties, i.e. `name`, `username`, `password` and pooling options.
3797+
3798+
To customize the connections created by a `ConnectionFactory`, i.e., set specific parameters that you do not want (or cannot) configure in your central database configuration, you can use a `ConnectionFactoryOptionsBuilderCustomizer` `@Bean`.
3799+
The following example shows how to manually override the database port while the rest of the options is taken from the application configuration:
3800+
3801+
[source,java,indent=0]
3802+
----
3803+
@Bean
3804+
public ConnectionFactoryOptionsBuilderCustomizer connectionFactoryPortCustomizer() {
3805+
return (builder) -> builder.option(PORT, 5432);
3806+
}
3807+
----
3808+
3809+
The following examples shows how to set some PostgreSQL connection options:
3810+
3811+
[source,java,indent=0]
3812+
----
3813+
@Bean
3814+
public ConnectionFactoryOptionsBuilderCustomizer postgresCustomizer() {
3815+
Map<String, String> options = new HashMap<>();
3816+
options.put("lock_timeout", "30s");
3817+
options.put("statement_timeout", "60s");
3818+
return (builder) -> builder.option(OPTIONS, options);
3819+
}
3820+
----
3821+
3822+
When a `ConnectionFactory` bean is available, the regular jdbc `DataSource` auto-configuration backs off.
3823+
If you need to initialize or migrate the database on startup, consider using the dedicated Flyway or Liquibase support.
3824+
Alternatively, consider registering a `ResourceDatabasePopulator` bean with the scripts to invoke on startup.
3825+
3826+
3827+
3828+
[[boot-features-r2dbc-embedded-database]]
3829+
==== Embedded Database Support
3830+
Similarly to <<boot-features-embedded-database-support,the JDBC support>>, Spring Boot can automatically configure an embedded database for reactive usage.
3831+
You need not provide any connection URLs.
3832+
You need only include a build dependency to the embedded database that you want to use, as shown in the following example:
3833+
3834+
[source,xml,indent=0]
3835+
----
3836+
<dependency>
3837+
<groupId>io.r2dbc</groupId>
3838+
<artifactId>r2dbc-h2</artifactId>
3839+
<scope>runtime</scope>
3840+
</dependency>
3841+
----
3842+
3843+
[NOTE]
3844+
====
3845+
If you are using this feature in your tests, you may notice that the same database is reused by your whole test suite regardless of the number of application contexts that you use.
3846+
If you want to make sure that each context has a separate embedded database, you should set `spring.r2dbc.generate-unique-name` to `true`.
3847+
====
3848+
3849+
3850+
3851+
[[boot-features-r2dbc-using-database-client]]
3852+
==== Using DatabaseClient
3853+
Spring Data's `DatabaseClient` class is auto-configured, and you can `@Autowire` it directly into your own beans, as shown in the following example:
3854+
3855+
[source,java,indent=0]
3856+
----
3857+
import org.springframework.beans.factory.annotation.Autowired;
3858+
import org.springframework.data.r2dbc.function.DatabaseClient;
3859+
import org.springframework.stereotype.Component;
3860+
3861+
@Component
3862+
public class MyBean {
3863+
3864+
private final DatabaseClient databaseClient;
3865+
3866+
@Autowired
3867+
public MyBean(DatabaseClient databaseClient) {
3868+
this.databaseClient = databaseClient;
3869+
}
3870+
3871+
// ...
3872+
3873+
}
3874+
----
3875+
3876+
3877+
3878+
[[boot-features-spring-data-r2dbc-repositories]]
3879+
==== Spring Data R2DBC Repositories
3880+
https://spring.io/projects/spring-data-r2dbc[Spring Data R2DBC] repositories are interfaces that you can define to access data.
3881+
Queries are created automatically from your method names.
3882+
For example, a `CityRepository` interface might declare a `findAllByState(String state)` method to find all the cities in a given state.
3883+
3884+
For more complex queries, you can annotate your method with Spring Data's {spring-data-r2dbc-api}/repository/query/Query.html[`Query`] annotation.
3885+
3886+
Spring Data repositories usually extend from the {spring-data-commons-api}/repository/Repository.html[`Repository`] or {spring-data-commons-api}/repository/CrudRepository.html[`CrudRepository`] interfaces.
3887+
If you use auto-configuration, repositories are searched from the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) down.
3888+
3889+
The following example shows a typical Spring Data repository interface definition:
3890+
3891+
[source,java,indent=0]
3892+
----
3893+
package com.example.myapp.domain;
3894+
3895+
import org.springframework.data.domain.*;
3896+
import org.springframework.data.repository.*;
3897+
import reactor.core.publisher.Mono;
3898+
3899+
public interface CityRepository extends Repository<City, Long> {
3900+
3901+
Mono<City> findByNameAndStateAllIgnoringCase(String name, String state);
3902+
3903+
}
3904+
----
3905+
3906+
TIP: We have barely scratched the surface of Spring Data R2DBC. For complete details, see the {spring-data-r2dbc-docs}[Spring Data R2DBC reference documentation].
3907+
3908+
3909+
37773910
[[boot-features-nosql]]
37783911
== Working with NoSQL Technologies
37793912
Spring Data provides additional projects that help you access a variety of NoSQL technologies, including:

0 commit comments

Comments
 (0)