1. Introduction
Database connection pooling is the approach where we use a pool of connections for database communication. This pool represents a number of open connections to the database that are reused for the database interaction. Connection pooling helps us to avoid the cost of establishing and closing connections. Also, this approach reduces the load on the database itself.
In this short tutorial, we'll show how to set a custom database connection pool size in Spring Boot.
2. Implementation
Spring Boot 1.x used Tomcat implementation of connection pooling. Spring Boot 2.x moved to HikariCP since it's very lightweight and configurable, and, also, exceptionally high-performant.
Every database management system can accept a certain amount of connections because managing connections takes a lot of resources. For MySQL, the default limit is 151 connections, but it can be configured to be much more. When working in a microservice environment, we should keep in mind the number of connections we're keeping and it would be good to adjust the number of connections according to our situation to keep resource usage as low as possible. By default, Spring Boot uses a pool size of 10 connections. To set a maximum custom connection pool size to, for example, 20 connections, we should use the following parameter:
spring.datasource.hikari.maximum-pool-size=20
3. Conclusion
In this short tutorial, we showed how to set a custom database connection pool size in Spring Boot.