官方文档:https://commons.apache.org/proper/commons-dbcp/configuration.html
下面讲的配置通常不会使用,采用默认值即可
- maxConnLifetimeMillis,默认值为-1。当该值大于0就会判断当前时间与该PooledObject创建时间差是否大于该值,是的话会抛出异常。用于commons-dbcp的validateObject、passivateObject、activateObject,这三个方法的调用者会使用cache或finally将回收PooledObject。官方说明:The maximum lifetime in milliseconds of a connection. After this time is exceeded the connection will fail the next activation, passivation or validation test. A value of zero or less means the connection has an infinite lifetime.
//在commons-dbcp包PoolableConnectionFactory
private void validateLifetime(final PooledObject<PoolableConnection> p)
throws Exception {
if (maxConnLifetimeMillis > 0) {
final long lifetime = System.currentTimeMillis() - p.getCreateTime();
if (lifetime > maxConnLifetimeMillis) {
throw new LifetimeExceededException(Utils.getMessage(
"connectionFactory.lifetimeExceeded",
Long.valueOf(lifetime),
Long.valueOf(maxConnLifetimeMillis)));
}
}
}
public boolean validateObject(final PooledObject<PoolableConnection> p) {
try {
validateLifetime(p);
validateConnection(p.getObject());
return true;
} catch (final Exception e) {
if (log.isDebugEnabled()) {
log.debug(Utils.getMessage(
"poolableConnectionFactory.validateObject.fail"), e);
}
return false;
}
}
//在commons-pool包GenericObjectPool
public void returnObject(final T obj) {
......
if (getTestOnReturn()) {
if (!factory.validateObject(p)) {
try {
destroy(p);
} catch (final Exception e) {
swallowException(e);
}
......
}
}
}
上面代码想说明在使用完连接返回连接池时,如果有设置testOnReturn为true,则会检查,其中会检查该连接的生命时长,如果超出maxConnLifetimeMillis则回收
- fastFailValidation,默认为false。设置为true,如果之前连接发生过某些错误(这个在下个配置说明),则再次进行验证时直接抛出异常,不再进行验证
//在commons-dbpc包PoolableConnection
public void validate(final String sql, int timeout) throws SQLException {
if (_fastFailValidation && _fatalSqlExceptionThrown) {
throw new SQLException(Utils.getMessage("poolableConnection.validate.fastFail"));
}
.....
//下面代码是关于验证是否可以连接上数据库,有两个方式
}
- disconnectionSqlCodes,默认为空,多个值默认采用逗号隔开。直接官方说明:Indicate that unrecoverable SQLException was thrown when using this connection. Such a connection should be considered broken and not pass validation in the future.
//在commons-dbcp包PoolableConnection
private boolean isDisconnectionSqlException(final SQLException e) {
boolean fatalException = false;
final String sqlState = e.getSQLState();
if (sqlState != null) {
fatalException = _disconnectionSqlCodes == null ? sqlState.startsWith(Utils.DISCONNECTION_SQL_CODE_PREFIX)
|| Utils.DISCONNECTION_SQL_CODES.contains(sqlState) : _disconnectionSqlCodes.contains(sqlState);
if (!fatalException) {
final SQLException nextException = e.getNextException();
if (nextException != null && nextException != e) {
fatalException = isDisconnectionSqlException(e.getNextException());
}
}
}
return fatalException;
}
- 关于removeAbandonedOnBorrow、removeAbandonedOnMaintenance、removeAbandonedTimeout 这三个配置通常不使用
- removeAbandonedOnMaintenance和removeAbandonedTimeout用在evict后台线程中是否触发抛弃方法(removeAbandoned),关闭PooledObject上次使用时间超过removeAbandonedTimeout
- removeAbandonedOnBorrow用在用池中获取连接时判断是否触发抛弃方法(removeAbandoned)
//在commons-pool包GenericObjectPool
public T borrowObject(final long borrowMaxWaitMillis) throws Exception {
assertOpen();
final AbandonedConfig ac = this.abandonedConfig;
if (ac != null && ac.getRemoveAbandonedOnBorrow() &&
(getNumIdle() < 2) &&
(getNumActive() > getMaxTotal() - 3) ) {
removeAbandoned(ac);
}
}
下星期写篇关于GenericObjectPool对池的管理