apache commons-pool2参数说明

commons-pool是java世界很常用的对象池框架,dbcp和jedis都使用commons-pool作为其自身连接池的基础。了解其各项参数的作用,对系统性能提升有较大的帮助。
以下参数来自commons-pool版本: 2.4.2

org.apache.commons.pool2.impl.BaseObjectPoolConfig:

  • lifo 默认true 后进先出
  • fairness 默认false Returns whether or not the pool serves threads waiting to borrow objects fairly. True means that waiting threads are served as if waiting in a FIFO queue. true if waiting threads are to be served by the pool in arrival order
  • maxWaitMillis 默认-1 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间 the maximum amount of time (in milliseconds) the borrowObject() method should block before throwing an exception when the pool is exhausted and getBlockWhenExhausted() is true. When less than 0, the borrowObject() method may block indefinitely.
  • minEvictableIdleTimeMillis 默认1000L * 60L * 30L 逐出连接的最小空闲时间 the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any - see setTimeBetweenEvictionRunsMillis(long)). When non-positive, no objects will be evicted from the pool due to idle time alone.
  • softMinEvictableIdleTimeMillis 默认1000L * 60L * 30L 对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略) the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any - see setTimeBetweenEvictionRunsMillis(long)), with the extra condition that at least minIdle object instances remain in the pool. This setting is overridden by getMinEvictableIdleTimeMillis() (that is, if getMinEvictableIdleTimeMillis() is positive, then getSoftMinEvictableIdleTimeMillis() is ignored).
  • numTestsPerEvictionRun 默认3 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n); the maximum number of objects to examine during each run (if any) of the idle object evictor thread. When positive, the number of tests performed for a run will be the minimum of the configured value and the number of idle instances in the pool. When negative, the number of tests performed will be ceil(getNumIdle()/ abs(getNumTestsPerEvictionRun())) which means that when the value is -n roughly one nth of the idle objects will be tested per run.
  • testOnCreate 默认false
  • testOnBorrow 默认false 在获取连接的时候检查有效性
  • testOnReturn 默认false
  • testWhileIdle 默认false 在空闲时检查有效性
  • timeBetweenEvictionRunsMillis 默认-1 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程 the number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.
  • evictionPolicyClassName 默认org.apache.commons.pool2.impl.DefaultEvictionPolicy 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
  • blockWhenExhausted 默认true 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时; true if borrowObject() should block when the pool is exhausted

org.apache.commons.pool2.impl.GenericObjectPoolConfig extends BaseObjectPoolConfig:

  • maxTotal 默认8 最大连接数; the maximum number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time. When negative, there is no limit to the number of objects that can be managed by the pool at one time.
  • maxIdle 默认8 最大空闲连接数

    最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止。
    空闲链接数大于maxIdle时,将进行回收
    maxIdle是最大的空闲连接数,这里取值为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除

  • minIdle 默认0 最小空闲连接数, 低于minIdle时,将创建新的链接

org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig extends BaseObjectPoolConfig

  • maxTotal 默认-1
  • maxTotalPerKey 默认8
  • minIdlePerKey 默认0
  • maxIdlePerKey 默认8

JedisPoolConfig extends GenericObjectPoolConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
package redis.clients.jedis;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
public class JedisPoolConfig extends GenericObjectPoolConfig {
public JedisPoolConfig() {
// defaults to make your life with connection pool easier :)
setTestWhileIdle(true);
setMinEvictableIdleTimeMillis(60000);
setTimeBetweenEvictionRunsMillis(30000);
setNumTestsPerEvictionRun(-1);
}
}

类关系图

部分参考: http://www.cnblogs.com/tankaixiong/p/4048167.html