To configure MySQL connection pooling in Spring Boot, add the MySQL driver, set the datasource URL and credentials, and tune HikariCP through spring.datasource.hikari.* properties. Spring Boot uses HikariCP by default whenever it is on the classpath, which it is through spring-boot-starter-jdbc and spring-boot-starter-data-jpa, so a working pool needs no Java code.
The settings that matter most are maximum-pool-size, minimum-idle, connection-timeout and max-lifetime. This guide uses Spring Boot 3 and MySQL Connector/J 8.x or 9.x.
Add the starter and the MySQL driver. Spring Boot manages the driver version through the mysql-connector-j artifact, which replaced the old mysql:mysql-connector-java coordinates.
Then configure the connection in application.yml.
The driver class com.mysql.cj.jdbc.Driver is detected from the URL, so you do not need driver-class-name. The old class com.mysql.jdbc.Driver no longer exists in current drivers.
Add the pool settings below the hikari key.
| Property | Meaning | Default |
|---|---|---|
maximum-pool-size | Maximum connections, idle plus in use | 10 |
minimum-idle | Idle connections HikariCP tries to keep | Same as maximum |
connection-timeout | Milliseconds a caller waits for a connection before failing | 30000 |
idle-timeout | How long an idle connection may live above the minimum | 600000 |
max-lifetime | Maximum age of any connection before it is retired | 1800000 |
keepalive-time | Interval for pinging idle connections | disabled |
leak-detection-threshold | Logs a warning if a connection is held longer than this | disabled |
HikariCP's own documentation recommends leaving minimum-idle unset so the pool is fixed-size, which gives the best performance and the most predictable load on the database. Set max-lifetime several seconds shorter than MySQL's wait_timeout and any firewall or proxy idle limit, otherwise the pool may hand out connections that the server already closed.
Bigger pools are not faster. A common starting point from the HikariCP project is a pool of roughly (core_count * 2) + effective_spindle_count connections for the database server, not for each application instance. For most services a size between 10 and 20 is enough.
When you run many application instances, multiply the pool size by the instance count and compare it with MySQL's max_connections. Ten instances with a pool of 20 need 200 connections plus headroom for administration and replicas. Measure with realistic load and raise the size only when threads wait for connections.
Pass driver-level options with data-source-properties.
Statement caching reduces parsing work, and rewriteBatchedStatements lets Connector/J turn JDBC batches into multi-row inserts, which can speed up bulk writes considerably. Test these on your workload before relying on them. Note that Hibernate batching also needs spring.jpa.properties.hibernate.jdbc.batch_size and an ID strategy that allows batching (IDENTITY disables insert batching).
Turn on Actuator metrics and the Hikari log.
Then read pool metrics such as hikaricp.connections.active, hikaricp.connections.pending and hikaricp.connections.timeout from /actuator/metrics. A non-zero pending value under normal load means requests are waiting for connections.
You can also inspect the pool from code.
max-lifetime below the server or firewall timeout and set keepalive-time.Connection is not available, request timed out after 30000ms: connections are leaked or the pool is too small. Enable leak-detection-threshold and check for code that holds a connection during slow remote calls.max_connections.open-in-view with long requests: the default spring.jpa.open-in-view=true keeps a connection for the whole web request in some flows. Set it to false for APIs.Spring Boot picks the pool in this order: HikariCP, Tomcat JDBC, Commons DBCP2, Oracle UCP. To use another one, exclude HikariCP and add the other dependency, or set spring.datasource.type explicitly. For most applications HikariCP is the right choice.
Yes. It is the first pool Spring Boot looks for and comes transitively with the JDBC and JPA starters.
Start with the default of 10 and measure. Raise it only when the pending metric shows threads waiting, and keep the total across instances under the database max_connections.
If MySQL closes a connection first, the pool may return a dead connection. A shorter max-lifetime makes HikariCP retire connections before the server does.
No. Spring Boot derives it from the JDBC URL. If you set it, use com.mysql.cj.jdbc.Driver.
Spring Boot gives you a HikariCP pool automatically. Set the URL and credentials, tune maximum-pool-size, max-lifetime and connection-timeout under spring.datasource.hikari, keep the pool small, and watch the Actuator Hikari metrics to confirm that threads are not waiting for connections.