Spring-Boot
JDBC
Connection Pool
Database Configuration
Application Properties

Spring-Boot How do I set JDBC pool properties like maximum number of connections?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Spring Boot is a powerful framework that streamlines the process of building Java applications. Among its many capabilities, Spring Boot offers excellent support for working with databases through JDBC (Java Database Connectivity). One common requirement in applications interacting with a database is the management of a pool of database connections. Proper configuration of these JDBC pool properties can have a significant impact on your application's performance and resource utilization.

Understanding JDBC Connection Pools

A JDBC connection pool is essentially a cache of database connections maintained so that connections can be reused when future requests to the database are required. Connection pools help in reducing the overhead of establishing a connection to the database, thereby improving performance and throughput. One of the most important properties when configuring a connection pool is the maximum number of connections.

Configuring JDBC Pool Properties in Spring Boot

Spring Boot provides several ways to configure JDBC connection pool properties. One of the most popular libraries for managing these connection pools is HikariCP, which is the default in Spring Boot 2.x and above. HikariCP is known for its high performance, compact design, and ease of use.

Application Properties Configuration

To set the JDBC pool properties, you typically modify the application.properties or application.yml file found in the src/main/resources directory of your Spring Boot project.

For application.properties, you might include entries like the following:

properties
1# Datasource settings
2spring.datasource.url=jdbc:mysql://localhost:3306/mydb
3spring.datasource.username=user
4spring.datasource.password=password
5spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
6
7# HikariCP settings
8spring.datasource.hikari.maximum-pool-size=10
9spring.datasource.hikari.minimum-idle=3
10spring.datasource.hikari.idle-timeout=30000
11spring.datasource.hikari.max-lifetime=120000
12spring.datasource.hikari.connection-timeout=30000

In the above configuration:

  • spring.datasource.hikari.maximum-pool-size: Defines the maximum number of connections in the pool.
  • spring.datasource.hikari.minimum-idle: Sets the minimum number of idle connections that the pool tries to maintain.
  • spring.datasource.hikari.idle-timeout: Specifies the maximum amount of time a connection is idle before being closed.
  • spring.datasource.hikari.max-lifetime: Indicates the maximum lifetime of a connection in the pool.
  • spring.datasource.hikari.connection-timeout: The maximum time to wait for a connection to be available before throwing an exception.

For application.yml, the configuration would look like this:

yaml
1spring:
2  datasource:
3    url: jdbc:mysql://localhost:3306/mydb
4    username: user
5    password: password
6    driver-class-name: com.mysql.cj.jdbc.Driver
7    hikari:
8      maximum-pool-size: 10
9      minimum-idle: 3
10      idle-timeout: 30000
11      max-lifetime: 120000
12      connection-timeout: 30000

Programmatic Configuration

Alternatively, you can configure these properties programmatically using Java configuration. This can be particularly useful when you need more dynamic or conditional configuration logic.

java
1import com.zaxxer.hikari.HikariDataSource;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4
5@Configuration
6public class DataSourceConfig {
7
8    @Bean
9    public HikariDataSource dataSource() {
10        HikariDataSource dataSource = new HikariDataSource();
11        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
12        dataSource.setUsername("user");
13        dataSource.setPassword("password");
14        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
15        dataSource.setMaximumPoolSize(10);
16        dataSource.setMinimumIdle(3);
17        dataSource.setIdleTimeout(30000);
18        dataSource.setMaxLifetime(120000);
19        dataSource.setConnectionTimeout(30000);
20        return dataSource;
21    }
22}

Benefits and Considerations

  • Performance: By adjusting the maximum pool size and other related properties, applications can handle more database operations efficiently.
  • Resource Optimization: Proper settings help in minimizing the resource consumption of the application.
  • Scalability: Tuning these properties can help in scaling applications to handle larger loads smoothly.

Key Points in Configuration

PropertyDescription
maximum-pool-sizeMax number of connections in the pool
minimum-idleMin number of idle connections in the pool
idle-timeoutTimeout for idle connections
max-lifetimeMax life of a connection before retirement
connection-timeoutMax wait time for a connection to be available

Conclusion

Configuring JDBC pool properties effectively is crucial for any Java application that interacts heavily with databases. By carefully setting the maximum number of connections and other critical properties, you can ensure optimal performance and resource management of your Spring Boot application. As applications scale, revisiting these settings might be necessary to accommodate changing requirements and workloads.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design