Data
==========
Table of Contents
[Back to index](index.html)
* [1. SQL Databases](#data.sql)
* [1.1. Configure a DataSource](#data.sql.datasource)
* [1.1.1. Embedded Database Support](#data.sql.datasource.embedded)
* [1.1.2. Connection to a Production Database](#data.sql.datasource.production)
* [1.1.3. DataSource Configuration](#data.sql.datasource.configuration)
* [1.1.4. Supported Connection Pools](#data.sql.datasource.connection-pool)
* [1.1.5. Connection to a JNDI DataSource](#data.sql.datasource.jndi)
* [1.2. Using JdbcTemplate](#data.sql.jdbc-template)
* [1.3. JPA and Spring Data JPA](#data.sql.jpa-and-spring-data)
* [1.3.1. Entity Classes](#data.sql.jpa-and-spring-data.entity-classes)
* [1.3.2. Spring Data JPA Repositories](#data.sql.jpa-and-spring-data.repositories)
* [1.3.3. Spring Data Envers Repositories](#data.sql.jpa-and-spring-data.envers-repositories)
* [1.3.4. Creating and Dropping JPA Databases](#data.sql.jpa-and-spring-data.creating-and-dropping)
* [1.3.5. Open EntityManager in View](#data.sql.jpa-and-spring-data.open-entity-manager-in-view)
* [1.4. Spring Data JDBC](#data.sql.jdbc)
* [1.5. Using H2’s Web Console](#data.sql.h2-web-console)
* [1.5.1. Changing the H2 Console’s Path](#data.sql.h2-web-console.custom-path)
* [1.6. Using jOOQ](#data.sql.jooq)
* [1.6.1. Code Generation](#data.sql.jooq.codegen)
* [1.6.2. Using DSLContext](#data.sql.jooq.dslcontext)
* [1.6.3. jOOQ SQL Dialect](#data.sql.jooq.sqldialect)
* [1.6.4. Customizing jOOQ](#data.sql.jooq.customizing)
* [1.7. Using R2DBC](#data.sql.r2dbc)
* [1.7.1. Embedded Database Support](#data.sql.r2dbc.embedded)
* [1.7.2. Using DatabaseClient](#data.sql.r2dbc.using-database-client)
* [1.7.3. Spring Data R2DBC Repositories](#data.sql.r2dbc.repositories)
* [2. Working with NoSQL Technologies](#data.nosql)
* [2.1. Redis](#data.nosql.redis)
* [2.1.1. Connecting to Redis](#data.nosql.redis.connecting)
* [2.2. MongoDB](#data.nosql.mongodb)
* [2.2.1. Connecting to a MongoDB Database](#data.nosql.mongodb.connecting)
* [2.2.2. MongoTemplate](#data.nosql.mongodb.template)
* [2.2.3. Spring Data MongoDB Repositories](#data.nosql.mongodb.repositories)
* [2.2.4. Embedded Mongo](#data.nosql.mongodb.embedded)
* [2.3. Neo4j](#data.nosql.neo4j)
* [2.3.1. Connecting to a Neo4j Database](#data.nosql.neo4j.connecting)
* [2.3.2. Spring Data Neo4j Repositories](#data.nosql.neo4j.repositories)
* [2.4. Solr](#data.nosql.solr)
* [2.4.1. Connecting to Solr](#data.nosql.solr.connecting)
* [2.5. Elasticsearch](#data.nosql.elasticsearch)
* [2.5.1. Connecting to Elasticsearch using REST clients](#data.nosql.elasticsearch.connecting-using-rest)
* [Connecting to Elasticsearch using RestHighLevelClient](#data.nosql.elasticsearch.connecting-using-rest.restclient)
* [Connecting to Elasticsearch using ReactiveElasticsearchClient](#data.nosql.elasticsearch.connecting-using-rest.webclient)
* [2.5.2. Connecting to Elasticsearch by Using Spring Data](#data.nosql.elasticsearch.connecting-using-spring-data)
* [2.5.3. Spring Data Elasticsearch Repositories](#data.nosql.elasticsearch.repositories)
* [2.6. Cassandra](#data.nosql.cassandra)
* [2.6.1. Connecting to Cassandra](#data.nosql.cassandra.connecting)
* [2.6.2. Spring Data Cassandra Repositories](#data.nosql.cassandra.repositories)
* [2.7. Couchbase](#data.nosql.couchbase)
* [2.7.1. Connecting to Couchbase](#data.nosql.couchbase.connecting)
* [2.7.2. Spring Data Couchbase Repositories](#data.nosql.couchbase.repositories)
* [2.8. LDAP](#data.nosql.ldap)
* [2.8.1. Connecting to an LDAP Server](#data.nosql.ldap.connecting)
* [2.8.2. Spring Data LDAP Repositories](#data.nosql.ldap.repositories)
* [2.8.3. Embedded In-memory LDAP Server](#data.nosql.ldap.embedded)
* [2.9. InfluxDB](#data.nosql.influxdb)
* [2.9.1. Connecting to InfluxDB](#data.nosql.influxdb.connecting)
* [3. What to Read Next](#data.whats-next)
Spring Boot integrates with a number of data technologies, both SQL and NoSQL.
[](#data.sql)1. SQL Databases
----------
The [Spring Framework](https://spring.io/projects/spring-framework) provides extensive support for working with SQL databases, from direct JDBC access using `JdbcTemplate` to complete “object relational mapping” technologies such as Hibernate.[Spring Data](https://spring.io/projects/spring-data) provides an additional level of functionality: creating `Repository` implementations directly from interfaces and using conventions to generate queries from your method names.
### [](#data.sql.datasource)1.1. Configure a DataSource ###
Java’s `javax.sql.DataSource` interface provides a standard method of working with database connections.
Traditionally, a 'DataSource' uses a `URL` along with some credentials to establish a database connection.
| |See [the “How-to” section](howto.html#howto.data-access.configure-custom-datasource) for more advanced examples, typically to take full control over the configuration of the DataSource.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#data.sql.datasource.embedded)1.1.1. 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](howto.html#howto.data-initialization).|
|---|----------------------------------------------------------------------------------------------------------------|
Spring Boot can auto-configure embedded [H2](https://www.h2database.com), [HSQL](http://hsqldb.org/), and [Derby](https://db.apache.org/derby/) databases.
You need not provide any connection URLs.
You need only include a build dependency to the embedded database that you want to use.
If there are multiple embedded databases on the classpath, set the `spring.datasource.embedded-database-connection` configuration property to control which one is used.
Setting the property to `none` disables auto-configuration of an embedded database.
| |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.datasource.generate-unique-name` to `true`.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
For example, the typical POM dependencies would be as follows:
```
org.springframework.boot
spring-boot-starter-data-jpa
org.hsqldb
hsqldb
runtime
```
| |You need a dependency on `spring-jdbc` for an embedded database to be auto-configured.
In this example, it is pulled in transitively through `spring-boot-starter-data-jpa`.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |If, for whatever reason, you do configure the connection URL for an embedded database, take care to ensure that the database’s automatic shutdown is disabled.
If you use H2, you should use `DB_CLOSE_ON_EXIT=FALSE` to do so.
If you use HSQLDB, you should ensure that `shutdown=true` is not used.
Disabling the database’s automatic shutdown lets Spring Boot control when the database is closed, thereby ensuring that it happens once access to the database is no longer needed.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#data.sql.datasource.production)1.1.2. Connection to a Production Database ####
Production database connections can also be auto-configured by using a pooling `DataSource`.
#### [](#data.sql.datasource.configuration)1.1.3. DataSource Configuration ####
DataSource configuration is controlled by external configuration properties in `spring.datasource.*`.
For example, you might declare the following section in `application.properties`:
Properties
```
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
```
Yaml
```
spring:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
```
| |You should at least specify the URL by setting the `spring.datasource.url` property.
Otherwise, Spring Boot tries to auto-configure an embedded database.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |Spring Boot can deduce the JDBC driver class for most databases from the URL.
If you need to specify a specific class, you can use the `spring.datasource.driver-class-name` property.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |For a pooling `DataSource` to be created, we need to be able to verify that a valid `Driver` class is available, so we check for that before doing anything.
In other words, if you set `spring.datasource.driver-class-name=com.mysql.jdbc.Driver`, then that class has to be loadable.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
See [`DataSourceProperties`](https://github.com/spring-projects/spring-boot/tree/v2.6.4/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java) for more of the supported options.
These are the standard options that work regardless of [the actual implementation](features.html#data.sql.datasource.connection-pool).
It is also possible to fine-tune implementation-specific settings by using their respective prefix (`spring.datasource.hikari.*`, `spring.datasource.tomcat.*`, `spring.datasource.dbcp2.*`, and `spring.datasource.oracleucp.*`).
See the documentation of the connection pool implementation you are using for more details.
For instance, if you use the [Tomcat connection pool](https://tomcat.apache.org/tomcat-9.0-doc/jdbc-pool.html#Common_Attributes), you could customize many additional settings, as shown in the following example:
Properties
```
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.test-on-borrow=true
```
Yaml
```
spring:
datasource:
tomcat:
max-wait: 10000
max-active: 50
test-on-borrow: true
```
This will set the pool to wait 10000ms before throwing an exception if no connection is available, limit the maximum number of connections to 50 and validate the connection before borrowing it from the pool.
#### [](#data.sql.datasource.connection-pool)1.1.4. Supported Connection Pools ####
Spring Boot uses the following algorithm for choosing a specific implementation:
1. We prefer [HikariCP](https://github.com/brettwooldridge/HikariCP) for its performance and concurrency.
If HikariCP is available, we always choose it.
2. Otherwise, if the Tomcat pooling `DataSource` is available, we use it.
3. Otherwise, if [Commons DBCP2](https://commons.apache.org/proper/commons-dbcp/) is available, we use it.
4. If none of HikariCP, Tomcat, and DBCP2 are available and if Oracle UCP is available, we use it.
| |If you use the `spring-boot-starter-jdbc` or `spring-boot-starter-data-jpa` “starters”, you automatically get a dependency to `HikariCP`.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------|
You can bypass that algorithm completely and specify the connection pool to use by setting the `spring.datasource.type` property.
This is especially important if you run your application in a Tomcat container, as `tomcat-jdbc` is provided by default.
Additional connection pools can always be configured manually, using `DataSourceBuilder`.
If you define your own `DataSource` bean, auto-configuration does not occur.
The following connection pools are supported by `DataSourceBuilder`:
* HikariCP
* Tomcat pooling `Datasource`
* Commons DBCP2
* Oracle UCP & `OracleDataSource`
* Spring Framework’s `SimpleDriverDataSource`
* H2 `JdbcDataSource`
* PostgreSQL `PGSimpleDataSource`
#### [](#data.sql.datasource.jndi)1.1.5. Connection to a JNDI DataSource ####
If you deploy your Spring Boot application to an Application Server, you might want to configure and manage your DataSource by using your Application Server’s built-in features and access it by using JNDI.
The `spring.datasource.jndi-name` property can be used as an alternative to the `spring.datasource.url`, `spring.datasource.username`, and `spring.datasource.password` properties to access the `DataSource` from a specific JNDI location.
For example, the following section in `application.properties` shows how you can access a JBoss AS defined `DataSource`:
Properties
```
spring.datasource.jndi-name=java:jboss/datasources/customers
```
Yaml
```
spring:
datasource:
jndi-name: "java:jboss/datasources/customers"
```
### [](#data.sql.jdbc-template)1.2. Using JdbcTemplate ###
Spring’s `JdbcTemplate` and `NamedParameterJdbcTemplate` classes are auto-configured, and you can `@Autowire` them directly into your own beans, as shown in the following example:
```
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final JdbcTemplate jdbcTemplate;
public MyBean(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void doSomething() {
this.jdbcTemplate ...
}
}
```
You can customize some properties of the template by using the `spring.jdbc.template.*` properties, as shown in the following example:
Properties
```
spring.jdbc.template.max-rows=500
```
Yaml
```
spring:
jdbc:
template:
max-rows: 500
```
| |The `NamedParameterJdbcTemplate` reuses the same `JdbcTemplate` instance behind the scenes.
If more than one `JdbcTemplate` is defined and no primary candidate exists, the `NamedParameterJdbcTemplate` is not auto-configured.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
### [](#data.sql.jpa-and-spring-data)1.3. JPA and Spring Data JPA ###
The Java Persistence API is a standard technology that lets you “map” objects to relational databases.
The `spring-boot-starter-data-jpa` POM provides a quick way to get started.
It provides the following key dependencies:
* Hibernate: One of the most popular JPA implementations.
* Spring Data JPA: Helps you to implement JPA-based repositories.
* Spring ORM: Core ORM support from the Spring Framework.
| |We do not go into too many details of JPA or [Spring Data](https://spring.io/projects/spring-data) here.
You can follow the [“Accessing Data with JPA”](https://spring.io/guides/gs/accessing-data-jpa/) guide from [spring.io](https://spring.io) and read the [Spring Data JPA](https://spring.io/projects/spring-data-jpa) and [Hibernate](https://hibernate.org/orm/documentation/) reference documentation.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#data.sql.jpa-and-spring-data.entity-classes)1.3.1. Entity Classes ####
Traditionally, JPA “Entity” classes are specified in a `persistence.xml` file.
With Spring Boot, this file is not necessary and “Entity Scanning” is used instead.
By default, all packages below your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) are searched.
Any classes annotated with `@Entity`, `@Embeddable`, or `@MappedSuperclass` are considered.
A typical entity class resembles the following example:
```
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class City implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
// ... additional members, often include @OneToMany mappings
protected City() {
// no-args constructor required by JPA spec
// this one is protected since it should not be used directly
}
public City(String name, String state) {
this.name = name;
this.state = state;
}
public String getName() {
return this.name;
}
public String getState() {
return this.state;
}
// ... etc
}
```
| |You can customize entity scanning locations by using the `@EntityScan` annotation.
See the “[howto.html](howto.html#howto.data-access.separate-entity-definitions-from-spring-configuration)” how-to.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#data.sql.jpa-and-spring-data.repositories)1.3.2. Spring Data JPA Repositories ####
[Spring Data JPA](https://spring.io/projects/spring-data-jpa) repositories are interfaces that you can define to access data.
JPA 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`](https://docs.spring.io/spring-data/jpa/docs/2.6.2/api/org/springframework/data/jpa/repository/Query.html) annotation.
Spring Data repositories usually extend from the [`Repository`](https://docs.spring.io/spring-data/commons/docs/2.6.2/api/org/springframework/data/repository/Repository.html) or [`CrudRepository`](https://docs.spring.io/spring-data/commons/docs/2.6.2/api/org/springframework/data/repository/CrudRepository.html) 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:
```
import org.springframework.boot.docs.data.sql.jpaandspringdata.entityclasses.City;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
public interface CityRepository extends Repository {
Page findAll(Pageable pageable);
City findByNameAndStateAllIgnoringCase(String name, String state);
}
```
Spring Data JPA repositories support three different modes of bootstrapping: default, deferred, and lazy.
To enable deferred or lazy bootstrapping, set the `spring.data.jpa.repositories.bootstrap-mode` property to `deferred` or `lazy` respectively.
When using deferred or lazy bootstrapping, the auto-configured `EntityManagerFactoryBuilder` will use the context’s `AsyncTaskExecutor`, if any, as the bootstrap executor.
If more than one exists, the one named `applicationTaskExecutor` will be used.
| |When using deferred or lazy bootstrapping, make sure to defer any access to the JPA infrastructure after the application context bootstrap phase.
You can use `SmartInitializingSingleton` to invoke any initialization that requires the JPA infrastructure.
For JPA components (such as converters) that are created as Spring beans, use `ObjectProvider` to delay the resolution of dependencies, if any.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |We have barely scratched the surface of Spring Data JPA.
For complete details, see the [Spring Data JPA reference documentation](https://docs.spring.io/spring-data/jpa/docs/2.6.2/reference/html).|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#data.sql.jpa-and-spring-data.envers-repositories)1.3.3. Spring Data Envers Repositories ####
If [Spring Data Envers](https://spring.io/projects/spring-data-envers) is available, JPA repositories are auto-configured to support typical Envers queries.
To use Spring Data Envers, make sure your repository extends from `RevisionRepository` as show in the following example:
```
import org.springframework.boot.docs.data.sql.jpaandspringdata.entityclasses.Country;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.history.RevisionRepository;
public interface CountryRepository extends RevisionRepository, Repository {
Page findAll(Pageable pageable);
}
```
| |For more details, check the [Spring Data Envers reference documentation](https://docs.spring.io/spring-data/envers/docs/2.6.2/reference/html/).|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#data.sql.jpa-and-spring-data.creating-and-dropping)1.3.4. Creating and Dropping JPA Databases ####
By default, JPA databases are automatically created **only** if you use an embedded database (H2, HSQL, or Derby).
You can explicitly configure JPA settings by using `spring.jpa.*` properties.
For example, to create and drop tables you can add the following line to your `application.properties`:
Properties
```
spring.jpa.hibernate.ddl-auto=create-drop
```
Yaml
```
spring:
jpa:
hibernate.ddl-auto: "create-drop"
```
| |Hibernate’s own internal property name for this (if you happen to remember it better) is `hibernate.hbm2ddl.auto`.
You can set it, along with other Hibernate native properties, by using `spring.jpa.properties.*` (the prefix is stripped before adding them to the entity manager).
The following line shows an example of setting JPA properties for Hibernate:|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Properties
```
spring.jpa.properties.hibernate[globally_quoted_identifiers]=true
```
Yaml
```
spring:
jpa:
properties:
hibernate:
"globally_quoted_identifiers": "true"
```
The line in the preceding example passes a value of `true` for the `hibernate.globally_quoted_identifiers` property to the Hibernate entity manager.
By default, the DDL execution (or validation) is deferred until the `ApplicationContext` has started.
There is also a `spring.jpa.generate-ddl` flag, but it is not used if Hibernate auto-configuration is active, because the `ddl-auto` settings are more fine-grained.
#### [](#data.sql.jpa-and-spring-data.open-entity-manager-in-view)1.3.5. Open EntityManager in View ####
If you are running a web application, Spring Boot by default registers [`OpenEntityManagerInViewInterceptor`](https://docs.spring.io/spring-framework/docs/5.3.16/javadoc-api/org/springframework/orm/jpa/support/OpenEntityManagerInViewInterceptor.html) to apply the “Open EntityManager in View” pattern, to allow for lazy loading in web views.
If you do not want this behavior, you should set `spring.jpa.open-in-view` to `false` in your `application.properties`.
### [](#data.sql.jdbc)1.4. Spring Data JDBC ###
Spring Data includes repository support for JDBC and will automatically generate SQL for the methods on `CrudRepository`.
For more advanced queries, a `@Query` annotation is provided.
Spring Boot will auto-configure Spring Data’s JDBC repositories when the necessary dependencies are on the classpath.
They can be added to your project with a single dependency on `spring-boot-starter-data-jdbc`.
If necessary, you can take control of Spring Data JDBC’s configuration by adding the `@EnableJdbcRepositories` annotation or a `JdbcConfiguration` subclass to your application.
| |For complete details of Spring Data JDBC, see the [reference documentation](https://docs.spring.io/spring-data/jdbc/docs/2.3.2/reference/html/).|
|---|------------------------------------------------------------------------------------------------------------------------------------------------|
### [](#data.sql.h2-web-console)1.5. Using H2’s Web Console ###
The [H2 database](https://www.h2database.com) provides a [browser-based console](https://www.h2database.com/html/quickstart.html#h2_console) that Spring Boot can auto-configure for you.
The console is auto-configured when the following conditions are met:
* You are developing a servlet-based web application.
* `com.h2database:h2` is on the classpath.
* You are using [Spring Boot’s developer tools](using.html#using.devtools).
| |If you are not using Spring Boot’s developer tools but would still like to make use of H2’s console, you can configure the `spring.h2.console.enabled` property with a value of `true`.|
|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |The H2 console is only intended for use during development, so you should take care to ensure that `spring.h2.console.enabled` is not set to `true` in production.|
|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#data.sql.h2-web-console.custom-path)1.5.1. Changing the H2 Console’s Path ####
By default, the console is available at `/h2-console`.
You can customize the console’s path by using the `spring.h2.console.path` property.
### [](#data.sql.jooq)1.6. Using jOOQ ###
jOOQ Object Oriented Querying ([jOOQ](https://www.jooq.org/)) is a popular product from [Data Geekery](https://www.datageekery.com/) which generates Java code from your database and lets you build type-safe SQL queries through its fluent API.
Both the commercial and open source editions can be used with Spring Boot.
#### [](#data.sql.jooq.codegen)1.6.1. Code Generation ####
In order to use jOOQ type-safe queries, you need to generate Java classes from your database schema.
You can follow the instructions in the [jOOQ user manual](https://www.jooq.org/doc/3.14.15/manual-single-page/#jooq-in-7-steps-step3).
If you use the `jooq-codegen-maven` plugin and you also use the `spring-boot-starter-parent` “parent POM”, you can safely omit the plugin’s `` tag.
You can also use Spring Boot-defined version variables (such as `h2.version`) to declare the plugin’s database dependency.
The following listing shows an example:
```
org.jooq
jooq-codegen-maven
...
com.h2database
h2
${h2.version}
org.h2.Driver
jdbc:h2:~/yourdatabase
...
```
#### [](#data.sql.jooq.dslcontext)1.6.2. Using DSLContext ####
The fluent API offered by jOOQ is initiated through the `org.jooq.DSLContext` interface.
Spring Boot auto-configures a `DSLContext` as a Spring Bean and connects it to your application `DataSource`.
To use the `DSLContext`, you can inject it, as shown in the following example:
```
import java.util.GregorianCalendar;
import java.util.List;
import org.jooq.DSLContext;
import org.springframework.stereotype.Component;
import static org.springframework.boot.docs.data.sql.jooq.dslcontext.Tables.AUTHOR;
@Component
public class MyBean {
private final DSLContext create;
public MyBean(DSLContext dslContext) {
this.create = dslContext;
}
}
```
| |The jOOQ manual tends to use a variable named `create` to hold the `DSLContext`.|
|---|--------------------------------------------------------------------------------|
You can then use the `DSLContext` to construct your queries, as shown in the following example:
```
public List authorsBornAfter1980() {
return this.create.selectFrom(AUTHOR)
.where(AUTHOR.DATE_OF_BIRTH.greaterThan(new GregorianCalendar(1980, 0, 1)))
.fetch(AUTHOR.DATE_OF_BIRTH);
```
#### [](#data.sql.jooq.sqldialect)1.6.3. jOOQ SQL Dialect ####
Unless the `spring.jooq.sql-dialect` property has been configured, Spring Boot determines the SQL dialect to use for your datasource.
If Spring Boot could not detect the dialect, it uses `DEFAULT`.
| |Spring Boot can only auto-configure dialects supported by the open source version of jOOQ.|
|---|------------------------------------------------------------------------------------------|
#### [](#data.sql.jooq.customizing)1.6.4. Customizing jOOQ ####
More advanced customizations can be achieved by defining your own `DefaultConfigurationCustomizer` bean that will be invoked prior to creating the `org.jooq.Configuration` `@Bean`.
This takes precedence to anything that is applied by the auto-configuration.
You can also create your own `org.jooq.Configuration` `@Bean` if you want to take complete control of the jOOQ configuration.
### [](#data.sql.r2dbc)1.7. Using R2DBC ###
The Reactive Relational Database Connectivity ([R2DBC](https://r2dbc.io)) project brings reactive programming APIs to relational databases.
R2DBC’s `io.r2dbc.spi.Connection` provides a standard method of working with non-blocking database connections.
Connections are provided by using a `ConnectionFactory`, similar to a `DataSource` with jdbc.
`ConnectionFactory` configuration is controlled by external configuration properties in `spring.r2dbc.*`.
For example, you might declare the following section in `application.properties`:
Properties
```
spring.r2dbc.url=r2dbc:postgresql://localhost/test
spring.r2dbc.username=dbuser
spring.r2dbc.password=dbpass
```
Yaml
```
spring:
r2dbc:
url: "r2dbc:postgresql://localhost/test"
username: "dbuser"
password: "dbpass"
```
| |You do not need to specify a driver class name, since Spring Boot obtains the driver from R2DBC’s Connection Factory discovery.|
|---|-------------------------------------------------------------------------------------------------------------------------------|
| |At least the url should be provided.
Information specified in the URL takes precedence over individual properties, that is `name`, `username`, `password` and pooling options.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| |The “How-to” section includes a [section on how to initialize a database](howto.html#howto.data-initialization.using-basic-sql-scripts).|
|---|----------------------------------------------------------------------------------------------------------------------------------------|
To customize the connections created by a `ConnectionFactory`, that is, set specific parameters that you do not want (or cannot) configure in your central database configuration, you can use a `ConnectionFactoryOptionsBuilderCustomizer` `@Bean`.
The following example shows how to manually override the database port while the rest of the options is taken from the application configuration:
```
import io.r2dbc.spi.ConnectionFactoryOptions;
import org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyR2dbcConfiguration {
@Bean
public ConnectionFactoryOptionsBuilderCustomizer connectionFactoryPortCustomizer() {
return (builder) -> builder.option(ConnectionFactoryOptions.PORT, 5432);
}
}
```
The following examples show how to set some PostgreSQL connection options:
```
import java.util.HashMap;
import java.util.Map;
import io.r2dbc.postgresql.PostgresqlConnectionFactoryProvider;
import org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyPostgresR2dbcConfiguration {
@Bean
public ConnectionFactoryOptionsBuilderCustomizer postgresCustomizer() {
Map options = new HashMap<>();
options.put("lock_timeout", "30s");
options.put("statement_timeout", "60s");
return (builder) -> builder.option(PostgresqlConnectionFactoryProvider.OPTIONS, options);
}
}
```
When a `ConnectionFactory` bean is available, the regular JDBC `DataSource` auto-configuration backs off.
If you want to retain the JDBC `DataSource` auto-configuration, and are comfortable with the risk of using the blocking JDBC API in a reactive application, add `@Import(DataSourceAutoConfiguration.class)` on a `@Configuration` class in your application to re-enable it.
#### [](#data.sql.r2dbc.embedded)1.7.1. Embedded Database Support ####
Similarly to [the JDBC support](features.html#data.sql.datasource.embedded), Spring Boot can automatically configure an embedded database 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, as shown in the following example:
```
io.r2dbc
r2dbc-h2
runtime
```
| |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`.|
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
#### [](#data.sql.r2dbc.using-database-client)1.7.2. Using DatabaseClient ####
A `DatabaseClient` bean is auto-configured, and you can `@Autowire` it directly into your own beans, as shown in the following example:
```
import java.util.Map;
import reactor.core.publisher.Flux;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final DatabaseClient databaseClient;
public MyBean(DatabaseClient databaseClient) {
this.databaseClient = databaseClient;
}
// ...
public Flux