Spring Boot 2.0如何替换默认HikariCP为Tomcat连接池?
Hey Luke, let's work through this problem together—this is a super common issue when migrating to Spring Boot 2.x, so you’re definitely not alone here!
Spring Boot 2.0+ uses HikariCP as the default datasource implementation out of the box. Just setting spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource in your application.yml won’t cut it if HikariCP is still present on your classpath. Here’s what you need to do to get Tomcat’s pool working:
Exclude HikariCP from your Spring Boot starter dependencies
In yourpom.xml, modify eitherspring-boot-starter-jdbcorspring-boot-starter-data-jpa(whichever you’re using) to exclude HikariCP:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </exclusion> </exclusions> </dependency>Add the Tomcat JDBC Pool dependency explicitly
Make sure you’ve got this dependency in yourpom.xml—Spring Boot won’t auto-include it unless you ask:<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency>Verify the datasource is actually switching
To confirm everything’s working, add a quick debug check in your code:@Autowired private DataSource dataSource; @PostConstruct public void confirmDataSourceType() { System.out.println("Active DataSource: " + dataSource.getClass().getName()); }If all goes well, this should print
org.apache.tomcat.jdbc.pool.DataSourcewhen your app starts.
If you’d rather stick with HikariCP (it’s actually the recommended pool for Spring Boot 2.x thanks to its performance), let’s fix the connection timeout problem instead. The most likely culprits are changed default settings or connection leaks:
Adjust Hikari’s default configuration
Spring Boot 2.x tweaked some Hikari defaults that can cause timeout issues. Try updating these settings in yourapplication.yml:spring: datasource: hikari: maximumPoolSize: 15 # Match this to your database's max allowed connections connectionTimeout: 10000 # Wait 10 seconds for a connection before timing out idleTimeout: 600000 # Close idle connections after 10 minutes maxLifetime: 1200000 # Set to less than your DB's connection timeout (e.g., 20 mins) leakDetectionThreshold: 2000 # Log a warning if a connection is held longer than 2 secondsCheck for connection leaks
TheleakDetectionThresholdsetting above will help you spot if your code is failing to close connections properly. Look for warnings in your logs about connections being held beyond the threshold—those are clues to where you might be missing atry-with-resourcesblock or not closing aConnection/EntityManager.Rule out database-side issues
Sometimes the problem isn’t the pool—it’s the database itself:- Check if your database is hitting its maximum connection limit.
- Look for long-running queries that are hogging connections.
- Verify your database isn’t dropping connections unexpectedly (check DB logs for timeout errors).
If switching to Tomcat’s pool is your priority, double-check that you’ve excluded HikariCP and added the Tomcat dependency correctly. If you decide to stick with HikariCP, adjusting the pool settings and hunting for connection leaks should resolve those timeout errors.
内容的提问来源于stack exchange,提问作者Luke

