Master Spring Boot DataSource: Configure HikariCP for Efficient DB Access
This article explains the concept of a DataSource, compares it with core JDBC APIs, and provides a step‑by‑step guide to configuring Spring Boot 2.x’s default HikariCP connection pool, including common properties, their meanings, and links to full example projects.
Basic Concepts
Before configuring a DataSource in Spring Boot, we need to understand the fundamentals of database access.
What is JDBC?
Java Database Connectivity (JDBC) is the standard API that defines how Java programs interact with relational databases. The core JDBC classes reside in the java.sql package, while extensions are in javax.sql.
DriverManager – loads drivers and provides connections.
Driver – the actual driver implementation.
Connection – represents a session with the database, creates Statement objects.
Statement / PreparedStatement / CallableStatement – execute static SQL, parameterized SQL, and stored procedures respectively.
SQLException – signals errors during connection or SQL execution.
What is a DataSource?
The DataSource interface is defined in javax.sql. It abstracts a pool of connections, allowing uniform management of connection parameters and efficient reuse through pooling.
Reasons to use a DataSource include:
Centralized configuration of database access parameters.
Connection‑pool management to reduce overhead and improve performance.
Popular open‑source DataSource implementations include DBCP, C3P0, Druid, and HikariCP.
Default DataSource: HikariCP
Spring Boot 2.x adopts HikariCP as the default connection pool. Most settings can be changed via properties; custom beans are only needed for special cases such as multiple DataSources.
General DataSource Properties
Common settings use the spring.datasource.* prefix, e.g.:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.DriverHikariCP Specific Properties
Pool‑specific settings use the spring.datasource.hikari.* prefix. Typical options:
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.idle-timeout=500000
spring.datasource.hikari.max-lifetime=540000
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.connection-test-query=SELECT 1Explanation of the above properties: minimum-idle: minimum idle connections (default 10). maximum-pool-size: maximum pool size (default 10 if non‑positive). idle-timeout: idle connection timeout; values < 10 s are reset to 10 s. max-lifetime: maximum lifetime of a connection; must be shorter than MySQL timeout. connection-timeout: timeout for acquiring a connection; values < 250 ms are reset to 30 s. connection-test-query: SQL used to validate connections.
For a complete list of HikariCP options, see the following tables:
Code Example
Full examples are available in the chapter3-2 directory of the project:
GitHub: https://github.com/dyc87112/SpringBoot-Learning/
Gitee: https://gitee.com/didispace/SpringBoot-Learning/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
