Quick revision
The short answer
Opening a database connection is expensive, because it needs a TCP connection, often a TLS handshake, authentication and a new server session, so a pool opens connections once and lends them out again and again. HikariCP is Spring Boot's default pool when it is on the classpath, which it is with spring-boot-starter-jdbc or data-jpa. maximumPoolSize caps the number of open connections (10 by default), connectionTimeout is how long a caller waits for a free one (30 seconds by default), and maxLifetime retires connections after a time (30 minutes by default) and should be shorter than any database or network idle limit; all are set under spring.datasource.hikari.*.
What to remember
In this answer, we saw that pools exist because opening a connection needs TCP, TLS, authentication and a server session, and that Spring Boot uses HikariCP by default when it is on the classpath. maximumPoolSize caps open connections at 10 by default and should be sized from database capacity, connectionTimeout limits the wait for a connection to 30 seconds by default and is often lowered, and maxLifetime retires connections after 30 minutes by default and must be shorter than any idle limit in the network or database. All are set under spring.datasource.hikari.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Opening a database connection is expensive, because it needs a TCP connection, often a TLS handshake, authentication and a new server session, so a pool opens connections once and lends them out again and again. HikariCP is Spring Boot's default pool when it is on the classpath, which it is with spring-boot-starter-jdbc or data-jpa. maximumPoolSize caps the number of open connections (10 by default), connectionTimeout is how long a caller waits for a free one (30 seconds by default), and maxLifetime retires connections after a time (30 minutes by default) and should be shorter than any database or network idle limit; all are set under spring.datasource.hikari.*.
Why this question is asked
Almost every Spring Boot service talks to a database through a connection pool, and most production database incidents involve it in some way: callers timing out waiting for a connection, connections dropped by a firewall, or too many connections for the database. Interviewers ask this to check that we know why pools exist and what the three most important settings really control. In this answer, we will see what a connection costs, how a pool works, how Spring Boot picks HikariCP, and then go through maximumPoolSize, connectionTimeout and maxLifetime with their defaults and how to set them.
1What Opening a Connection Costs
A database connection is not just an object. To create one, the driver opens a TCP connection to the server, usually negotiates TLS, which needs extra round trips and cryptographic work, and then authenticates, for example with SCRAM password exchange on PostgreSQL. The server then sets up a session: PostgreSQL starts a whole new backend process for each connection. Altogether that can take from a few milliseconds on a local network to tens of milliseconds across zones.
If every request opened its own connection, a query that takes 2 ms would spend most of its time connecting, and the database would spend its CPU creating and destroying sessions. A pool avoids that by keeping a set of connections open and lending them out.
6 messages in order. Play them, or step through them one at a time.
The expensive connect happens once; each request only borrows and returns.
2How a Pool Works and How Spring Boot Picks HikariCP
A pool holds a number of open physical connections. When code calls dataSource.getConnection(), the pool hands out an idle one wrapped in a proxy. When the code calls close() on that proxy, the pool does not close the physical connection; it rolls back anything left uncommitted, resets settings like auto-commit, and marks the connection idle again. In the background, the pool opens new connections when needed, up to its maximum, retires old ones, and checks that idle ones still work.
Spring Boot's DataSource auto-configuration looks for a pool on the classpath in a fixed order and uses HikariCP first if present. spring-boot-starter-jdbc and spring-boot-starter-data-jpa both bring HikariCP as a dependency, so in practice every Boot application uses it unless we exclude it or set spring.datasource.type to another pool. HikariCP is popular because it is small, fast and has sensible defaults.
3MaximumPoolSize: How Many Connections at Most
maximumPoolSize is the largest number of connections the pool will hold, both in use and idle. The default is 10. HikariCP's minimumIdle defaults to the same value, which makes a fixed-size pool, the recommended setup, so the service holds 10 connections from shortly after startup.
Bigger is not better. A database can only work on as many queries at once as it has CPU cores and disk bandwidth; past that, more connections just queue inside the database and add contention. The HikariCP documentation suggests starting from a formula of about twice the number of database cores plus the number of effective disks, and measuring from there. It is also a budget: 20 instances with 10 connections each means 200 connections on the database, which must stay below its max_connections setting, 100 by default in PostgreSQL.
4ConnectionTimeout: How Long a Caller Waits
When all connections are in use, a caller of getConnection() waits for one to be returned. connectionTimeout is how long it waits before giving up with an SQLException. The default is 30,000 ms, 30 seconds, and the minimum is 250 ms.
Thirty seconds is long for an online request; the user has usually given up and the thread has been blocked the whole time. Many services lower it to a few seconds so that an overloaded service fails fast, returns an error and recovers, rather than piling up waiting threads. The right value is a little longer than the normal worst-case wait, and shorter than the request's own timeout.
5MaxLifetime: When Connections Are Retired
maxLifetime is the longest time a connection may live in the pool. When a connection reaches it, HikariCP closes it as soon as it is not in use and opens a replacement; a connection that is in use is never closed under the caller. The default is 1,800,000 ms, 30 minutes, and HikariCP adds a small random variation so connections do not all expire at once.
The reason for it is that something else may close connections silently. Firewalls, load balancers and NAT gateways often drop TCP connections that have been idle for a time, and databases or proxies may enforce their own limits. If the pool kept a connection longer than such a limit, the next borrower would get a dead connection and an error. So maxLifetime should be several seconds or minutes shorter than any such limit. HikariCP also has idleTimeout (10 minutes by default, used only when minimumIdle is below the maximum) and keepaliveTime, which pings idle connections to keep them alive; check your HikariCP version for its default.
6Configuring and Watching the Pool
In Spring Boot, all HikariCP properties are available under spring.datasource.hikari, in kebab-case as above, and Boot binds them to the HikariDataSource it creates. With Actuator and Micrometer, the pool also publishes metrics such as hikaricp.connections.active, hikaricp.connections.idle, hikaricp.connections.pending and hikaricp.connections.acquire, tagged with the pool name. Pending connections above zero for long periods, or acquire times growing, are the early warning that the pool is too small or that connections are held too long, well before callers start timing out.
| Setting | Default | Controls |
|---|---|---|
maximumPoolSize | 10 | most connections open at once |
minimumIdle | same as maximumPoolSize | idle connections kept ready |
connectionTimeout | 30,000 ms | how long getConnection() waits |
maxLifetime | 1,800,000 ms (30 min) | age at which a connection is retired |
idleTimeout | 600,000 ms (10 min) | idle time before removal, if below max |
leakDetectionThreshold | 0 (off) | logs connections held longer than this |
spring.datasource.hikari.* in Spring Boot.7Follow-up questions
Should the pool be as large as the number of request threads?
Usually not. Most of a request's time is often spent outside the database, so far fewer connections than threads are needed, and a pool larger than the database can serve just moves the queue into the database. We size it from the database's capacity and the measured concurrency of database work, and let other requests wait briefly for a connection.
What happens at startup if the database is down?
By default HikariCP tries to open a connection during startup and fails the application start if it cannot within initializationFailTimeout, which defaults to 1 ms after the first attempt. That fail-fast behaviour is usually what we want in containers, where the orchestrator restarts the service until the database is reachable.
Why does a fixed-size pool help?
With minimumIdle equal to maximumPoolSize, the pool does not have to open connections during a traffic spike, which is exactly when opening them is most expensive and slow. It also gives the database a predictable number of connections from each instance, which makes the overall connection budget easy to reason about.
8Conclusion
In this answer, we saw that pools exist because opening a connection needs TCP, TLS, authentication and a server session, and that Spring Boot uses HikariCP by default when it is on the classpath. maximumPoolSize caps open connections at 10 by default and should be sized from database capacity, connectionTimeout limits the wait for a connection to 30 seconds by default and is often lowered, and maxLifetime retires connections after 30 minutes by default and must be shorter than any idle limit in the network or database. All are set under spring.datasource.hikari.
Open with
A pool exists because opening a database connection is expensive. It means a TCP connection, usually a TLS handshake, authentication, and on PostgreSQL a whole new server process.
Then, if they want more
Doing that per request would cost more than the query itself, so the pool opens connections once and lends them out; when my code closes a pooled connection, it actually goes back to the pool after being reset. Spring Boot uses HikariCP by default whenever it is on the classpath, and the JDBC and JPA starters bring it in.
The three settings: maximumPoolSize is the cap on open connections, ten by default, and HikariCP recommends a fixed-size pool.
Bigger is not better, because the database can only run so many queries at once, and every instance's pool adds to the database's total connection count. connectionTimeout is how long a caller waits for a free connection before getting an exception, thirty seconds by default; I would usually lower it to a few seconds so an overloaded service fails fast. maxLifetime is the maximum age of a connection, thirty minutes by default; the pool retires connections when they reach it, never while in use, and it should be shorter than any idle timeout in firewalls, load balancers or the database so we never hand out a connection that was silently dropped.
All of them go under spring.datasource.hikari, and I would watch the pool's active, idle and pending metrics through Actuator.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.sql, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why does a Spring Boot application use a connection pool, which pool is the default, and...": https://scrivio-interview-library.netlify.app/databases/jdbc/e41e530b0b/