Skip to content
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@mp911de @spring-operator
163 lines (126 sloc) 6.07 KB

Configure a R2DBC ConnectionFactory

R2DBC’s io.r2dbc.spi.Connection interface provides a standard method of working with non-blocking database connections . Traditionally, a 'ConnectionFactory' uses a URL along with some credentials to establish a database connection.

See the “How-to” section for more advanced examples, typically to take full control over the configuration of the ConnectionFactory.

Embedded Database Support

It is often convenient to develop applications by using an in-memory embedded database. Obviously, in-memory databases do not provide persistent storage. You need to populate your database when your application starts and be prepared to throw away data when your application ends.

The “How-to” section includes a section on how to initialize a database.

Spring Boot can auto-configure embedded H2, for reactive usage. You need not provide any connection URLs. You need only include a build dependency to the embedded database that you want to use.


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. If you want to make sure that each context has a separate embedded database, you should set spring.r2dbc.generate-unique-name to true.


Embedded databases are typically used through JDBC. Using R2DBC H2 gives you access to the same embedded database instance and you can access data through JDBC and R2DBC.

For example, the typical POM dependencies would be as follows:


Connection to a Production Database

Production database connections can also be auto-configured by using a pooling ConnectionFactory.

Additional connection pools can always be configured manually. If you define your own ConnectionFactory bean, auto-configuration does not occur.

ConnectionFactory configuration is controlled by external configuration properties in spring.r2dbc.*. For example, you might declare the following section in

You should at least specify the URL by setting the spring.r2dbc.url property. Otherwise, Spring Boot tries to auto-configure an embedded database.
You do not need to specify a driver class name, since Spring Boot obtains the driver from R2DBC’s Connection Factory discovery.

Using DatabaseClient

Spring Data’s DatabaseClient class is auto-configured, and you can @Autowire it directly into your own beans, as shown in the following example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class MyBean {

	private final DatabaseClient databaseClient;

	public MyBean(DatabaseClient databaseClient) {
		this.databaseClient = databaseClient;

	// ...


Spring Data R2DBC Repositories

Spring Data R2DBC repositories are interfaces that you can define to access data. Queries are created automatically from your method names. For example, a CityRepository interface might declare a findAllByState(String state) method to find all the cities in a given state.

For more complex queries, you can annotate your method with Spring Data’s Query annotation.

Spring Data repositories usually extend from the Repository or ReactiveCrudRepository interfaces. If you use auto-configuration, repositories are searched from the package containing your main configuration class (the one annotated with @EnableAutoConfiguration or @SpringBootApplication) down.

The following example shows a typical Spring Data repository interface definition:

package com.example.myapp.domain;

import reactor.core.publisher.Mono.

public interface CityRepository extends Repository<City, Long> {

	Mono<City> findByNameAndStateAllIgnoringCase(String name, String state);

We have barely scratched the surface of Spring Data R2DBC. For complete details, see the Spring Data R2DBC reference documentation.

Configure a Custom ConnectionFactory

To configure your own ConnectionFactory, define a @Bean of that type in your configuration. Spring Boot reuses your ConnectionFactory anywhere one is required, including database initialization. If you need to externalize some settings, you can bind your ConnectionFactory to the environment (see “spring-boot-features.adoc”).

The following example shows how to define a data source in a bean:

public ConnectionFactory connectionFactory() {
	return new FancyConnectionFactory();
You can’t perform that action at this time.