Spring Boot auto-configures everything you need to connect to a relational database, but many real-world scenarios—multiple data sources, custom pool settings, reactive stacks—require deliberate configuration choices. This guide answers the most common data access questions with concrete steps and configuration examples drawn directly from the Spring Boot source.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt
Use this file to discover all available pages before exploring further.
Configure a DataSource with URL, username, and password
Configure a DataSource with URL, username, and password
The simplest way to configure a Spring Boot can deduce the driver class for most databases from the JDBC URL. To specify a driver explicitly, use You can also bind a custom configuration prefix using a dedicated
DataSource is to set the spring.datasource.* properties in application.properties or application.yaml. Spring Boot will auto-detect the JDBC driver from the URL and create a pooled DataSource automatically.application.yaml
You must set at least
spring.datasource.url. Without it, Spring Boot attempts to auto-configure an embedded in-memory database instead.spring.datasource.driver-class-name.If you need a programmatic approach, use DataSourceBuilder to construct a DataSource from code:MyDataSourceConfiguration.java
DataSourceProperties bean, which handles the url-to-jdbc-url translation that Hikari requires:application.yaml
Use two DataSources in one application
Use two DataSources in one application
When you need a second Configure both data sources in your properties:To inject the second data source, annotate the injection point with the same
DataSource alongside the auto-configured one, declare it as a bean with defaultCandidate=false and qualify it with @Qualifier so it can be injected selectively:MyAdditionalDataSourceConfiguration.java
application.yaml
@Qualifier("second") annotation.Setting
defaultCandidate=false prevents the second DataSource from interfering with Spring Boot’s auto-configuration, which expects exactly one primary DataSource candidate.Use an in-memory H2 database for development
Use an in-memory H2 database for development
Spring Boot can auto-configure embedded H2, HSQL, and Derby databases. Add the H2 dependency to your classpath and omit the Spring Boot detects H2 on the classpath and auto-configures an in-memory To ensure each test application context gets its own separate embedded database, set:H2 also provides a browser-based web console that Spring Boot can auto-configure during development when Spring Boot DevTools is on the classpath. You can enable it explicitly with
spring.datasource.url property—no other configuration is needed:pom.xml
DataSource automatically.When multiple embedded databases are on the classpath, control which one is used with:application.yaml
application.yaml
spring.h2.console.enabled=true.Configure HikariCP connection pool settings
Configure HikariCP connection pool settings
Spring Boot defaults to HikariCP when it is on the classpath (included transitively with Spring Boot selects connection pool implementations in this order: HikariCP → Tomcat pooling
spring-boot-starter-jdbc or spring-boot-starter-data-jpa). Configure pool settings using the spring.datasource.hikari.* prefix:application.yaml
DataSource → Commons DBCP2 → Oracle UCP. To force a specific pool, set spring.datasource.type:application.yaml
Use JdbcTemplate directly
Use JdbcTemplate directly
Spring Boot auto-configures You can also use the newer
JdbcTemplate and NamedParameterJdbcTemplate. Inject either directly into your beans:MyBean.java
JdbcClient API, which is auto-configured based on the presence of NamedParameterJdbcTemplate:application.yaml
NamedParameterJdbcTemplate reuses the same JdbcTemplate instance behind the scenes. If more than one JdbcTemplate is defined and no primary candidate exists, NamedParameterJdbcTemplate is not auto-configured.Configure Spring Data JPA (show-sql, DDL auto)
Configure Spring Data JPA (show-sql, DDL auto)
Add Common values for
Pass arbitrary Hibernate properties through To enable deferred bootstrapping of JPA repositories (useful for reducing startup time):
spring-boot-starter-data-jpa to your project. Spring Boot auto-configures Hibernate as the JPA provider, a DataSource, and an EntityManagerFactory.Use spring.jpa.* properties to tune JPA behavior:application.yaml
spring.jpa.hibernate.ddl-auto:| Value | Behavior |
|---|---|
none | No schema action (default for production databases) |
validate | Validate schema but make no changes |
update | Update schema to match entities |
create | Create schema, dropping existing tables first |
create-drop | Create on startup, drop on shutdown (default for embedded databases) |
spring.jpa.properties.*:application.yaml
application.yaml
Use a custom EntityManagerFactory
Use a custom EntityManagerFactory
To take full control of When using JPA with multiple data sources, define one Configure the additional JPA namespace in your properties:Each
EntityManagerFactory configuration, define a bean named entityManagerFactory. Spring Boot backs off its auto-configuration when it detects a bean of that name.When you create your own
LocalContainerEntityManagerFactoryBean, use the auto-configured EntityManagerFactoryBuilder to preserve JPA and vendor properties—especially naming strategies and DDL mode settings from spring.jpa.*.EntityManagerFactory per data source:MyAdditionalEntityManagerFactoryConfiguration.java
application.yaml
EntityManagerFactory also needs a corresponding JpaTransactionManager, or you can use a JTA transaction manager that spans multiple data sources.Use R2DBC for reactive data access
Use R2DBC for reactive data access
R2DBC provides reactive, non-blocking access to relational databases. Configure the Add the R2DBC driver for your database to the classpath. For PostgreSQL:For embedded databases in reactive applications, use the R2DBC-specific driver:A
ConnectionFactory using spring.r2dbc.* properties:application.yaml
pom.xml
pom.xml
DatabaseClient bean is auto-configured and can be injected directly into your beans for reactive database access. Spring Data R2DBC repositories are also supported—define interfaces that extend Repository or CrudRepository and Spring Boot will auto-configure implementations for them.When a
ConnectionFactory bean is present, the regular JDBC DataSource auto-configuration backs off. To use both JDBC and R2DBC in the same application, add @Import(DataSourceAutoConfiguration.class) to a @Configuration class.