Tomcat连接池配置咨询:需200个不被替换的常驻连接
Hey there! I see you're dealing with connections getting replaced in your Tomcat JDBC pool and want a fixed pool of 200 persistent connections. Let's walk through adjusting your configuration to solve this.
The Core Issue with Your Current Setup
Your current config sets initialSize, minIdle, and maxActive to 150, but without parameters to prevent idle connection eviction or keep connections validated, Tomcat's pool might still recycle connections (either because the pool thinks they're idle too long, or the database closes stale connections).
Modified Configuration for a Stable 200-Connection Pool
Here's the adjusted config that keeps 200 connections persistent and prevents unnecessary replacement:
<Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" initialSize="200" minIdle="200" maxActive="200" maxWait="5000" poolPreparedStatements="true" username="user" password="password" driverClassName="com.jcc.Driver" url="jdbc:db2://XXX.XXX.XXX.XXX:50000/mydb" testWhileIdle="true" timeBetweenEvictionRunsMillis="3600000" minEvictableIdleTimeMillis="86400000" validationQuery="SELECT 1 FROM SYSIBM.SYSDUMMY1" removeAbandoned="false" />
Key Parameter Explanations
Let's break down the critical changes:
initialSize="200": Creates 200 connections immediately when the pool starts, so you don't have to wait for connections to be created on demand.minIdle="200"&maxActive="200": Setting these to the same value locks the pool size at 200. The pool will never shrink below 200 connections, so no connections get removed to reduce the pool size.testWhileIdle="true": Periodically checks idle connections to make sure they're still valid (prevents the database from closing stale connections without the pool knowing).timeBetweenEvictionRunsMillis="3600000": Runs the idle connection check every hour (adjust if needed, but this is a reasonable interval).minEvictableIdleTimeMillis="86400000": Only considers connections idle for 24+ hours for eviction. Combined withminIdle=200, this effectively prevents any connections from being evicted.validationQuery="SELECT 1 FROM SYSIBM.SYSDUMMY1": A simple DB2-specific query to validate connections (required fortestWhileIdleto work).removeAbandoned="false": Disables automatic cleanup of "abandoned" connections, ensuring no connections get removed unexpectedly.
With these settings, your pool will maintain exactly 200 connections that won't be replaced unnecessarily. Let me know if you run into any issues!
内容的提问来源于stack exchange,提问作者Prog Art

