eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Introduction

In this tutorial, we’ll discuss the best strategies for sizing the JDBC connection pool.

2. What Is a JDBC Connection Pool, and Why Is It Used?

A JDBC connection pool is a mechanism used to manage database connections efficiently. Creating a database connection involves several time-consuming steps, such as:

  • opening a connection to the database
  • authenticating the user
  • creating a TCP socket for communication
  • sending and receiving data over the socket
  • closing the connection and the TCP socket

Repeating these steps for each user request can be inefficient, especially for applications with many users. A JDBC connection pool addresses this problem by creating a pool of reusable connections ahead of time. When the application starts, it creates and maintains database connections in a pool. A Pool Connection Manager manages these connections and handles their lifecycle.

When a client requests a connection, the Pool Manager provides one from the pool instead of creating a new one. Once the client is done, the connection is returned to the pool for reuse rather than being closed. This reuse of connections saves time and resources, significantly improving application performance.

3. Why Is Deciding an Optimal Size for the JDBC Connection Pool Important?

Deciding the optimal size for a JDBC connection pool is crucial for balancing performance and resource utilization. A small pool might result in faster connection access but could lead to delays if there aren’t enough connections to satisfy all requests. Conversely, a large pool ensures that more connections are available, reducing the time spent in queues but potentially slowing down access to the connection table.

The following table summarises the pros and cons to consider when sizing connection pools:

Connection Pool Size Pros Cons
Small Pool Faster access to the connection table We may need more connections to satisfy requests. Requests may spend more time in the queue.
Large Pool More connections to fulfill requests. Requests spend less (or no) time in the queue Slower access to the connection table

4. Key Points to Consider While Deciding the JDBC Connection Pool Size

When deciding on the pool size, we need to consider several factors. First, we should evaluate the average transaction response time and the amount of time spent on database queries. Load testing can help determine these times, and it’s advisable to calculate the pool size with an additional 25% capacity to handle unexpected loads. Second, the connection pool should be capable of growing and shrinking based on actual needs. We can also monitor the system using logging statements or JMX surveillance to adjust the pool size dynamically.

Additionally, we should consider how many queries are executed per page load and the duration of each query. For optimal performance, we start with a few connections and gradually increase. A pool with 8 to 16 connections per node is often optimal. We can also adjust the Idle Timeout and Pool Resize Quantity values based on monitoring statistics.

5. Basic Settings That Control the JDBC Connection Pool

These basic settings control the pool size:

Connection Pool Property Description
Initial and Minimum Pool Size The size of the pool when it is created and its minimum allowable size
Maximum Pool Size The upper limit of the pool size
Pool Resize Quantity The number of connections to be removed when the idle timeout expires. Connections idle for longer than the timeout are candidates for removal, stopping once the pool reaches the initial and minimum pool size
Max Idle Connections The maximum number of idle connections allowed in the pool. If the number of idle connections exceeds this limit, the excess connections are closed, freeing up resources
Min Idle Connections The minimum number of idle connections to keep in the pool
Max Wait Time The maximum time an application will wait for a connection to become available
Validation Query A SQL query used to validate connections before they are handed over to the application

6. Best Practices for Sizing the JDBC Connection Pool

Here are some best practices for tuning the JDBC connection pool to ensure healthy connectivity to the database instance.

6.1. Validate Connection SQL

First, we add a validation SQL query to enable connection validation. It ensures connections in the pool are tested periodically and remain healthy. It also quickly detects and logs database failures, allowing administrators to act immediately. Choose the best validation method (e.g., metadata or table queries). After sufficient monitoring, switching off the connection validation can provide a further performance boost. Below is an example for configuring it using the most popular JDBC connection pool i.e. Hikari:

HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:your_database_url");
config.setUsername("your_database_username");
config.setPassword("your_database_password");
config.setConnectionTestQuery("SELECT 1");
HikariDataSource dataSource = new HikariDataSource(config);

6.2. Maximum Pool Size

We can also tune the maximum pool size to be sufficient for the number of users using the application. It is necessary to handle unusual loads. Even in non-production environments, multiple simultaneous connections may be needed:

config.setMaximumPoolSize(20);

6.3. Minimum Pool Size

We should then tune the minimum pool size to suit the application’s load. If multiple applications/servers connect to the same database, having a sufficient minimum pool size ensures reserved DB connections are available:

config.setMinimumIdle(10);

6.4. Connection Timeout

An additional aspect we can consider is configuring the blocking timeout in milliseconds so that connection requests wait for a connection to become available without waiting indefinitely. A rational value of 3 to 5 seconds is recommended:

config.setConnectionTimeout(5000);

6.5. Idle Timeout

Set the idle timeout to a value that suits the application. For shared databases, a lower idle timeout (e.g., 30 seconds) can make idle connections available for other applications. A higher value (e.g., 120 seconds) can prevent frequent connection creation for dedicated databases:

config.setIdleTimeout(30000);

// or

config.setIdleTimeout(120000);

6.6. Thread Pool Tuning

Use Load/Stress testing to estimate minimum and maximum pool sizes. A commonly used formula to estimate pool size is connections = (2 * core_count) + number_of_disks. This formula provides a starting point, but requirements might differ based on I/O blocking and other factors. Start with a small pool size and gradually increase it based on testing. Monitoring tools like pg_stat_activity can help determine if connections are idling too much, indicating a need to reduce the pool size:

int coreCount = Runtime.getRuntime().availableProcessors();
int numberOfDisks = 2; // assuming two no. of disc
int connections = (2 * coreCount) + numberOfDisks;
config.setMaximumPoolSize(connections);
config.setMinimumIdle(connections / 2);

6.7. Transaction Isolation Level

Choose the best-performing isolation level that meets concurrency and consistency needs. Avoid specifying the isolation level unless necessary. If specified, set the Isolation Level Guaranteed to false for the applications that do not change the isolation level programmatically:

try (Connection conn = dataSource.getConnection()) {
    conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
} catch (SQLException e) {
    e.printStackTrace();
}

7. Conclusion

In this article, we discuss how to set up the JDBC connection pool. By understanding the factors influencing connection pool size and following best practices for tuning and monitoring, we can ensure healthy database connectivity and improved application performance.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)