Skip to main content

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.

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.
The simplest way to configure a 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
spring:
  datasource:
    url: "jdbc:mysql://localhost/test"
    username: "dbuser"
    password: "dbpass"
You must set at least spring.datasource.url. Without it, Spring Boot attempts to auto-configure an embedded in-memory database instead.
Spring Boot can deduce the driver class for most databases from the JDBC URL. To specify a driver explicitly, use spring.datasource.driver-class-name.If you need a programmatic approach, use DataSourceBuilder to construct a DataSource from code:
MyDataSourceConfiguration.java
@Bean
public DataSource dataSource() {
    return DataSourceBuilder.create()
        .url("jdbc:mysql://localhost/test")
        .username("dbuser")
        .password("dbpass")
        .build();
}
You can also bind a custom configuration prefix using a dedicated DataSourceProperties bean, which handles the url-to-jdbc-url translation that Hikari requires:
application.yaml
app:
  datasource:
    url: "jdbc:mysql://localhost/test"
    username: "dbuser"
    password: "dbpass"
    configuration:
      maximum-pool-size: 30
When you need a second 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
@Bean
@Qualifier("second")
@ConfigurationProperties("app.datasource")
public DataSourceProperties secondDataSourceProperties() {
    return new DataSourceProperties();
}

@Bean
@Qualifier("second")
@ConfigurationProperties("app.datasource.configuration")
public HikariDataSource secondDataSource(
        @Qualifier("second") DataSourceProperties properties) {
    return properties.initializeDataSourceBuilder()
        .type(HikariDataSource.class)
        .build();
}
Configure both data sources in your properties:
application.yaml
spring:
  datasource:
    url: "jdbc:mysql://localhost/first"
    username: "dbuser"
    password: "dbpass"
    configuration:
      maximum-pool-size: 30
app:
  datasource:
    url: "jdbc:mysql://localhost/second"
    username: "dbuser"
    password: "dbpass"
    max-total: 30
To inject the second data source, annotate the injection point with the same @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.
Spring Boot can auto-configure embedded H2, HSQL, and Derby databases. Add the H2 dependency to your classpath and omit the spring.datasource.url property—no other configuration is needed:
pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
Spring Boot detects H2 on the classpath and auto-configures an in-memory DataSource automatically.
If you explicitly configure the H2 connection URL, add DB_CLOSE_ON_EXIT=FALSE to disable automatic shutdown and let Spring Boot manage the database lifecycle: jdbc:h2:mem:mydb;DB_CLOSE_ON_EXIT=FALSE.
When multiple embedded databases are on the classpath, control which one is used with:
application.yaml
spring:
  datasource:
    embedded-database-connection: h2
To ensure each test application context gets its own separate embedded database, set:
application.yaml
spring:
  datasource:
    generate-unique-name: true
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.h2.console.enabled=true.
Never set spring.h2.console.enabled=true in production. The H2 console is intended for development only.
Spring Boot defaults to HikariCP when it is on the classpath (included transitively with spring-boot-starter-jdbc or spring-boot-starter-data-jpa). Configure pool settings using the spring.datasource.hikari.* prefix:
application.yaml
spring:
  datasource:
    url: "jdbc:postgresql://localhost/mydb"
    username: "dbuser"
    password: "dbpass"
    hikari:
      maximum-pool-size: 20
      minimum-idle: 5
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000
Spring Boot exposes Hikari-specific settings under spring.datasource.hikari.*. If you define a custom DataSource bean with a different prefix, use spring.datasource.hikari.* for the pool settings and your custom prefix for the connection coordinates.
Spring Boot selects connection pool implementations in this order: HikariCP → Tomcat pooling DataSource → Commons DBCP2 → Oracle UCP. To force a specific pool, set spring.datasource.type:
application.yaml
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
Spring Boot auto-configures JdbcTemplate and NamedParameterJdbcTemplate. Inject either directly into your beans:
MyBean.java
@Component
public class MyBean {

    private final JdbcTemplate jdbcTemplate;

    public MyBean(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void doSomething() {
        this.jdbcTemplate.update("INSERT INTO users (name) VALUES (?)", "Alice");
    }
}
You can also use the newer JdbcClient API, which is auto-configured based on the presence of NamedParameterJdbcTemplate:
application.yaml
spring:
  jdbc:
    template:
      max-rows: 500
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.
Add 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:
    show-sql: true
    hibernate:
      ddl-auto: "validate"
      naming:
        physical-strategy: "com.example.MyPhysicalNamingStrategy"
Common values for spring.jpa.hibernate.ddl-auto:
ValueBehavior
noneNo schema action (default for production databases)
validateValidate schema but make no changes
updateUpdate schema to match entities
createCreate schema, dropping existing tables first
create-dropCreate on startup, drop on shutdown (default for embedded databases)
Spring Boot does not apply relaxed binding to properties under spring.jpa.properties.*. Property names must exactly match those expected by your JPA provider. For example, use spring.jpa.properties.hibernate.jdbc.batch_size, not batchSize or batch-size.
Pass arbitrary Hibernate properties through spring.jpa.properties.*:
application.yaml
spring:
  jpa:
    properties:
      hibernate:
        globally_quoted_identifiers: "true"
To enable deferred bootstrapping of JPA repositories (useful for reducing startup time):
application.yaml
spring:
  data:
    jpa:
      repositories:
        bootstrap-mode: deferred
To take full control of 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.*.
When using JPA with multiple data sources, define one EntityManagerFactory per data source:
MyAdditionalEntityManagerFactoryConfiguration.java
@Bean
@ConfigurationProperties("app.jpa")
@Bean(defaultCandidate = false)
public JpaProperties secondJpaProperties() {
    return new JpaProperties();
}

@Bean(defaultCandidate = false)
public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(
        @Qualifier("second") DataSource dataSource,
        @Qualifier("secondJpaProperties") JpaProperties jpaProperties,
        EntityManagerFactoryBuilder builder) {
    return builder
        .dataSource(dataSource)
        .properties(jpaProperties.getProperties())
        .packages(Order.class)
        .build();
}
Configure the additional JPA namespace in your properties:
application.yaml
app:
  jpa:
    properties:
      hibernate:
        default_schema: "orders"
Each EntityManagerFactory also needs a corresponding JpaTransactionManager, or you can use a JTA transaction manager that spans multiple data sources.
R2DBC provides reactive, non-blocking access to relational databases. Configure the ConnectionFactory using spring.r2dbc.* properties:
application.yaml
spring:
  r2dbc:
    url: "r2dbc:postgresql://localhost/test"
    username: "dbuser"
    password: "dbpass"
You do not need to specify a driver class name. Spring Boot obtains the driver from R2DBC’s Connection Factory discovery mechanism. Information in the URL takes precedence over individual properties.
Add the R2DBC driver for your database to the classpath. For PostgreSQL:
pom.xml
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>r2dbc-postgresql</artifactId>
    <scope>runtime</scope>
</dependency>
For embedded databases in reactive applications, use the R2DBC-specific driver:
pom.xml
<dependency>
    <groupId>io.r2dbc</groupId>
    <artifactId>r2dbc-h2</artifactId>
    <scope>runtime</scope>
</dependency>
A 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.

Build docs developers (and LLMs) love