Spring Boot DB connection pool

We have summarized how to set up a connection pool with Spring Boot (Spring Data JPA).

Automatic selection of libraries

The connection pool libraries are selected in the following order. (Check if there is a library in the classpath, select that library if it exists, and check the next library if not.)

  1. HikariCP
  2. Tomcat JDBC Connection Pool
  3. Commons DBCP2

If you are using spring-boot-starter-jdbc or spring-boot-starter-data-jpa, HikariCP will be resolved as a dependency, so if nothing is set, HikariCP will be selected. To.

How to specify the library

It can be changed by specifying the DataSource (javax.sql.DataSource inheritance class) of the library you want to use in the spring.datasource.type property of ʻapplication.properties (or ʻapplications.yaml).

For Hikari CP

See Configuration for the items that can be set.

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/test_db
    username: user
    password: password
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      maximum-pool-size: 20
      minimum-idle: 10
For Tomcat JDBC Connection Pool

See Common Attributes for the items that can be set.

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/test_db
    username: user
    password: password
    type: org.apache.tomcat.jdbc.pool.DataSource
    tomcat:
      max-active: 20
      min-idle: 10

reference

Recommended Posts

Spring Boot DB connection pool
Spring Boot DB related tips
db connection notes
Challenge Spring Boot
DB connection method-JDBC
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Understand how to share Spring DB connection (DB transaction)
Spring boot memo writing (1)
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
[Spring Boot] Environment construction (macOS)
Set context-param in Spring Boot
Spring + MyBatis connection setting method
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Spring Boot on Microsoft Azure
Implement GraphQL with Spring Boot
Try DB connection with Java
Spring Boot tutorial task schedule
Spring 5 & Spring Boot 2 Hands-on preparation procedure
Get started with Spring boot
DB connection settings Spring-Boot application.properties
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
[Spring Boot] Web application creation
spring boot port duplication problem
Run LIFF with Spring Boot
SNS login with Spring Boot
Spring Boot Hot Swapping settings
[Java] Thymeleaf Basic (Spring Boot)
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
Introduction to Spring Boot ② ~ AOP ~
CICS-Run Java application-(4) Spring Boot application
Spring Boot starting with Docker
Spring Boot + Springfox springfox-boot-starter 3.0.0 Use
Error in Spring database connection
Set cookies with Spring Boot
[Spring Boot] Easy paging recipe
Use Spring JDBC with Spring Boot
Docker × Spring Boot environment construction
Add module with Spring Boot
Getting Started with Spring Boot
NoHttpResponseException in Spring Boot + WireMock
[Spring Boot] Send an email
Spring Boot performance related settings
Introduction to Spring Boot Part 1