You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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!

1. Fixing the Tomcat Connection Pool Not Taking Effect

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 your pom.xml, modify either spring-boot-starter-jdbc or spring-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 your pom.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.DataSource when your app starts.

2. Troubleshooting the Original HikariCP Timeout Issue

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 your application.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 seconds
    
  • Check for connection leaks
    The leakDetectionThreshold setting 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 a try-with-resources block or not closing a Connection/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).
Final Thoughts

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:03:28