dbcp-BasicDataSource和PoolDataSource和GenericObjectPool之间的关系?

285 阅读1分钟

BasicDataSource

获取连接

public Connection getConnection() throws SQLException {
  return this.createDataSource().getConnection();1.创建数据源2.获取连接
}
protected volatile GenericObjectPool connectionPool = null; //连接池对象,作用是管理连接对象:1.从连接池获取连接对象 2.归还连接到连接池
protected volatile DataSource dataSource = null; //PoolDataSource
protected synchronized DataSource createDataSource() throws SQLException {
  if (this.closed) {
    throw new SQLException("Data source is closed");
  } else if (this.dataSource != null) {
    return this.dataSource;
  } else {
    ConnectionFactory driverConnectionFactory = this.createConnectionFactory();
    this.createConnectionPool(); //创建连接池对象
    GenericKeyedObjectPoolFactory statementPoolFactory = null;
    if (this.isPoolPreparedStatements()) {
      statementPoolFactory = new GenericKeyedObjectPoolFactory((KeyedPoolableObjectFactory)null, -1, (byte)0, 0L, 1, this.maxOpenPreparedStatements);
    }

    this.createPoolableConnectionFactory(driverConnectionFactory, statementPoolFactory, this.abandonedConfig);
    this.createDataSourceInstance(); //创建PoolDataSource

    try {
      for(int i = 0; i < this.initialSize; ++i) {
        this.connectionPool.addObject();
      }
    } catch (Exception var4) {
      throw new SQLNestedException("Error preloading the connection pool", var4);
    }

    return this.dataSource; //PoolDataSource
  }
}

BasicDataSource包含了PoolDataSource,即PoolDataSource是BasicDataSource的一个属性。


创建PoolDataSource

protected void createDataSourceInstance() throws SQLException {
  PoolingDataSource pds = new PoolingDataSource(this.connectionPool); //创建PoolDataSource
  pds.setAccessToUnderlyingConnectionAllowed(this.isAccessToUnderlyingConnectionAllowed());
  pds.setLogWriter(this.logWriter);
  this.dataSource = pds;
}

总结 主要作用是 1.创建连接池对象GenericObjectPool 2.创建PoolDataSource

PoolDataSource

public class PoolingDataSource implements DataSource {
  protected ObjectPool _pool; //连接池对象

PoolDataSource包含了连接池对象。

总结

BasicDataSource封装了PoolDataSource,PoolDataSource又封装了GenericObjectPool。

GenericObjectPool是最终的连接池对象,主要作用 1.从连接池获取连接和归还连接到连接池 2.连接池的各种配置参数

protected void createConnectionPool() { //创建连接池对象的时候,设置了各种配置参数
  Object gop;
  if (this.abandonedConfig != null && this.abandonedConfig.getRemoveAbandoned()) {
    gop = new AbandonedObjectPool((PoolableObjectFactory)null, this.abandonedConfig);
  } else {
    gop = new GenericObjectPool();
  }

  ((GenericObjectPool)gop).setMaxActive(this.maxActive);
  ((GenericObjectPool)gop).setMaxIdle(this.maxIdle);
  ((GenericObjectPool)gop).setMinIdle(this.minIdle);
  ((GenericObjectPool)gop).setMaxWait(this.maxWait);
  ((GenericObjectPool)gop).setTestOnBorrow(this.testOnBorrow);
  ((GenericObjectPool)gop).setTestOnReturn(this.testOnReturn);
  ((GenericObjectPool)gop).setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis);
  ((GenericObjectPool)gop).setNumTestsPerEvictionRun(this.numTestsPerEvictionRun);
  ((GenericObjectPool)gop).setMinEvictableIdleTimeMillis(this.minEvictableIdleTimeMillis);
  ((GenericObjectPool)gop).setTestWhileIdle(this.testWhileIdle);
  this.connectionPool = (GenericObjectPool)gop;
}

类继承图

BasicDataSource和PoolDataSource,都实现了DataSource接口。

PoolDataSource是BasicDataSource的数据。