Spring Boot 2.x Tomcat连接池+Oracle数据源配置求助
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
typeproperty: This is non-negotiable—without it, Spring Boot will still default to HikariCP, and your Tomcat settings will be completely ignored. - Tweak
validation-queryfor your database: Different databases require different validation queries. For example:- PostgreSQL:
SELECT 1 - Oracle:
SELECT 1 FROM DUAL - SQL Server:
SELECT 1
- PostgreSQL:
- Reuse your existing Tomcat config values: Most of the keys you used in Spring 1.5.x will work seamlessly under the
spring.datasource.tomcatnamespace—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

