优化Tomcat连接池配置需结合硬件资源、应用负载及数据库特性,核心参数调整如下:
基础线程池配置
连接池资源管理
timeBetweenEvictionRunsMillis
(默认5秒)定期回收空闲连接。连接有效性保障
true
,获取连接时校验有效性,配合validationQuery
(如SELECT 1
)确保连接可用。true
,自动回收超时连接(removeAbandonedTimeout
设为300秒),防止连接泄漏。性能优化参数
ConnectionState
拦截器,自动管理连接状态(如自动提交、只读)。监控与调优
配置示例(Spring Boot):
spring:
datasource:
tomcat:
max-active: 300
min-idle: 50
max-idle: 150
max-wait: 60000
test-on-borrow: true
validation-query: SELECT 1
time-between-eviction-runs-millis: 5000
remove-abandoned: true
remove-abandoned-timeout: 300
关键原则:
logAbandoned
,避免日志冗余。