# 数据源配置说明
# user-config.xml中配置
# c3p0数据源配置
除了eos自己定义的特殊的配置(C3p0-为前缀),其他的都可以以c3p0.为前缀进行配置,如果有重合会覆盖C3p0-为前缀的配置。下面是示例:
<group name="default">
<configValue key="Database-Type">MySql</configValue>
<configValue key="Jdbc-Type"/>
<configValue key="C3p0-DriverClass">com.mysql.jdbc.Driver</configValue>
<configValue key="C3p0-Url">jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false</configValue>
<configValue key="C3p0-UserName">root</configValue>
<configValue key="C3p0-Password">root</configValue>
<!-- 初始连接数 -->
<configValue key="C3p0-PoolSize">200</configValue>
<!-- 最小连接数 -->
<configValue key="C3p0-MinPoolSize">200</configValue>
<!-- 最大连接数 -->
<configValue key="C3p0-MaxPoolSize">300</configValue>
<!-- 最大空闲时间,单位秒,0表示连接永不过期 -->
<configValue key="C3p0-MaxIdleTime">600</configValue>
<!-- 空闲监测周期,单位秒 -->
<configValue key="C3p0-IdleConnectionTestPeriod">900</configValue>
<!-- 辅助线程数,用于执行后台任务(如连接测试、清理、Statement 缓存回收等) -->
<configValue key="C3p0-NumHelperThreads">5</configValue>
<!-- PreparedStatement缓存最大语句数,0表示不缓存 -->
<configValue key="C3p0-MaxStatements">0</configValue>
<!-- 控制当连接池中没有可用连接时,客户端等待获取连接的最长时间,单位毫秒 -->
<configValue key="c3p0.checkoutTimeout">3000</configValue>
<!-- 检测连接是否有效的 SQL -->
<configValue key="c3p0.preferredTestQuery">SELECT 1</configValue>
<configValue key="Transaction-Isolation">ISOLATION_DEFAULT</configValue>
<configValue key="Test-Connect-Sql">SELECT count(*) from EOS_UNIQUE_TABLE</configValue>
<configValue key="Retry-Connect-Count">-1</configValue>
</group>
c3p0各常用参数详细说明:
# 当连接池耗尽时,一次性创建的新连接数(默认值:3)。例如设为 5,则一次补 5 个连接,提升高并发响应速度。
c3p0.acquireIncrement=3
# 获取连接时的超时时间(单位:毫秒,默认值:0,表示永不超时,无限等待)
# 客户端调用 getConnection() 时,若无可用连接,最大等待时间(单位:毫秒)。设为 3000 表示最多等 3 秒,超时抛出 SQLException。
# 注意:如果acquireRetryAttempts * acquireRetryDelay > checkoutTimeout,则实际超时由 checkoutTimeout 优先控制。
c3p0.checkoutTimeout=3000
# 获取新连接失败时的重试次数。≤0 表示无限重试。(默认值:30)
c3p0.acquireRetryAttempts=30
# 每次重试之间的间隔时间(单位:毫秒,默认值:1000)
c3p0.acquireRetryDelay=1000
# 连接在池中空闲的最长时间(单位:秒,默认值:0,表示永不回收)
# 超过此时间未被使用的连接将被丢弃(前提是连接数 > minPoolSize)
c3p0.maxIdleTime=1800
# 连接的总生存时间(秒,默认值:0,表示不限制)。从创建起算,超过即断开(即使正在使用,也等其 close() 后断开)。
c3p0.maxConnectionAge=0
# 空闲连接检测周期(单位:秒,默认值:0,表示永不检测)。每隔 N 秒对空闲连接执行有效性测试(如执行 SELECT 1),防止数据库主动断连(如 MySQL 8 小时超时)。建议设为 300(5 分钟)。
c3p0.idleConnectionTestPeriod=300
# 用于测试连接有效性的 SQL 语句(如 MySQL 可用 "SELECT 1")
c3p0.preferredTestQuery=SELECT 1
# 强制回收未显式 close() 的连接(单位:秒,默认值:0,表示永不回收)。设为非 0 可防连接泄漏,但需确保业务逻辑在超时前完成。
c3p0.unreturnedConnectionTimeout=0
# 配合上一项,打印未归还连接(没有调用connection.close())的堆栈(仅调试用,影响性能)。(默认 false)
c3p0.debugUnreturnedConnectionStackTraces=false
# 从池中获取连接前是否进行有效性测试(默认 false)
# 每次 getConnection() 时测试连接有效性。性能损耗大,不建议开启。
c3p0.testConnectionOnCheckout=false
# 将连接返回池时是否测试有效性(默认 false)
# 每次 connection.close() 归还时测试。一般也不需要。
c3p0.testConnectionOnCheckin=false
# PreparedStatement缓存最大语句数,0表示不缓存(默认值:0)
c3p0.maxStatements=0
# 每个连接上缓存的 PreparedStatement 数量(默认值:0,不缓存)
# 若设为正数,将启用语句缓存
c3p0.maxStatementsPerConnection=0