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

Spring Boot 2.x Tomcat连接池+Oracle数据源配置求助

Fixing Tomcat Connection Pool Configuration in Spring Boot 2.x (via application.yml)

First off, let's break down why you're hitting this error: Spring Boot 2.x switched its default connection pool from Tomcat JDBC to HikariCP. That means your old Tomcat-specific config keys are being ignored (or causing conflicts) because Boot is trying to use Hikari instead. No worries, we can fix this with just a few tweaks to your application.yml and a quick dependency adjustment.

Step 1: Ensure Tomcat JDBC Dependency is Active

Spring Boot 2.x includes HikariCP by default, so we need to exclude it and explicitly add the Tomcat JDBC pool dependency to your build file.

Maven (pom.xml)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <exclusions>
        <!-- Exclude the default HikariCP dependency -->
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Add Tomcat's JDBC pool explicitly -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
</dependency>

Gradle (build.gradle)

implementation('org.springframework.boot:spring-boot-starter-jdbc') {
    exclude group: 'com.zaxxer', module: 'HikariCP'
}
implementation 'org.apache.tomcat:tomcat-jdbc'

Step 2: Configure Tomcat Pool in application.yml

The critical change here is explicitly telling Spring Boot to use the Tomcat DataSource, then nesting your familiar Tomcat pool settings under the tomcat sub-node. This matches the structure you used in Spring 1.5.8, so you won't have to rewrite all your config values.

Here's a complete, ready-to-use example (adjust the database details and pool values to match your setup):

spring:
  datasource:
    # Mandatory: Override default HikariCP with Tomcat's DataSource class
    type: org.apache.tomcat.jdbc.pool.DataSource
    url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
    username: your_db_username
    password: your_db_password
    # Tomcat pool-specific configurations (mirrors your Spring 1.5.x setup)
    tomcat:
      initial-size: 5          # Initial number of connections in the pool
      max-active: 20           # Maximum number of active connections allowed
      max-idle: 10             # Maximum number of idle connections to keep
      min-idle: 5              # Minimum number of idle connections to maintain
      max-wait: 10000          # Max wait time (in ms) for a connection before throwing an error
      test-on-borrow: true     # Validate connections when borrowing from the pool
      validation-query: SELECT 1  # Query to check connection health (adjust for your DB: e.g., Oracle uses "SELECT 1 FROM DUAL")
      validation-interval: 30000  # How often (in ms) to run validation checks on idle connections

Key Pitfalls to Avoid

  • Don't skip the type property: This is non-negotiable—without it, Spring Boot will still default to HikariCP, and your Tomcat settings will be completely ignored.
  • Tweak validation-query for your database: Different databases require different validation queries. For example:
    • PostgreSQL: SELECT 1
    • Oracle: SELECT 1 FROM DUAL
    • SQL Server: SELECT 1
  • Reuse your existing Tomcat config values: Most of the keys you used in Spring 1.5.x will work seamlessly under the spring.datasource.tomcat namespace—no need to learn new Hikari-specific settings if you prefer Tomcat's pool behavior.

That's all! With these changes, your Tomcat connection pool will work exactly like it did in Spring Boot 1.5.8, all configured directly through application.yml.

内容的提问来源于stack exchange,提问作者Anand Varkey Philips

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:33:57