DBCP 源码初探

1、DBCP工具实现Java.sql.DataSource接口的类BasicDataSource 分析:
DataSource dataSource = new BasicDataSource(); //创建DBCP数据源实例对象

BasicDataSource类的成员变量:
保存了连接池的全部配置参数,构造函数BasicDataSource()对所有成员变量赋予了默认值,并对外提供了get、set方法来修改配置参数。如下摘取部成员变量被赋予的默认值:

protected boolean defaultAutoCommit  = true;
protected  Boolean defaultReadOnly = null;
protected  String driverClassName = null;
protected  int maxActive = 0;
protected  int maxIdle= 8;
protected  int minIdle= 0;
protected  int initialSize = 0;
protected  int maxWait = -1L;
protected  String password= null;
protected  String url= null;
protected  String username= null;
private boolean restartNeeded= false;
protected  GenericObjectPool connectionPool;  // 连接池对象,其属性 _pool 是List数据类型,保存了连接池中的连接对象。
protected Properties connectionProperties = new Properties();
protected DataSource  dataSource = null;
protected PrinterWriter logWriter  = new PrinterWriter(System.out);

//初始化成员变量dataSource
protected synchronized DataSource createDataSource() throws SQLException {
    if ( this.dataSource != null ) {
       // 判断是否已经实例化数据源实例对象变量
        return this.dataSource;     
    }
   ......
   // 实例化连接池
   if (this.abandonedConfig != null && XXX ) {
       this.connectionPool = new AbandonedObjectPool(null, 
         this.abandonedConfig);
   } else
   this.connectionPool = new GenericObjectPool(); 
   //connectionPool 拷贝BasicDataSource中的配置参数
   this.connectionPool.setMaxActive(this.maxActive);
   this.connectionPool.setMaxIdle(this.maxIdle);
   this.connectionPool.setMinIdle(this.minIdle);
   this.connectionPool.setMaxWait(this.maxWait);
   this.connectionPool.setTestOnBorrow(this.testOnBorrow);
   this.connectionPool.setTestOnReturn(this.testOnReturn);
   ......
   //实例化数据源实例对象
   this.dataSource = new PoolingDataSource(this.connectionPool); 
   //往连接池中添加connection对象
   for( int i=0; i < this.initialSize; ++i) {
            this.connectionPool.addObject();   
        }
    return this.dataSource;
}

//dataSource(PoolingDataSource)的getConnection方法获取连接
public Connection getConnection() throws SQLException {
    return createDataSource().getConnection();  
}

//BasicDataSource关闭连接池方法和重启方法
public synchronized void close() {
    GenericObjectPool oldpool =  this.connectionPool;
    this.connectionPool = null; //置空连接池
    this.dataSource = null;   //置空数据源实例对象
    try{
        oldpool .close();       // 关闭连接池
    }
    ......
}

private void restart() {
    this.close();
}

BasicDataSource的成员变量connectionPool(GenericObjectPool):
GenericObjectPool介绍:

public static final byte WHEN_EXHAUSTED_FAIL = 0;
......
//下面几个属性拷贝自BasicDataSource
private int _maxIdle;
private int _minIdle;
private int _maxActive;
private long _maxWait;
private volatible boolean _testOnBorrow;
private volatible boolean _testOnReturn;

private int _numActive;   //保存当前活跃的connection数量

//class CursorableLinkedList implements List,Serializable
private CursorableLinkedList _pool;
......
// 获取一个连接对象
public Object borrowObject() throws Exception {
    GenericKeyedObjectPool.ObjectTimestampPair pair = null;
    synchronized (this) {
        while (true) {
            //从List中移除连接
            pair = this._pool.removeFirst();
            ......
        }
        ......
        this._numActive += 1;
        ......
        return pair.value;
        ......
    }
}

//清空连接池
public synchronized void clear() {
    for (Iterator it = this._pool.iterator(); it.hasNext();) {
        this._factory.destroyObject(it.next().value);
    }
}

//返还一个连接对象
public void returnObject(Object obj) throws Exception{
    ......
    addObjectToPool(obj,true);
    ......
    synchronized(this) {
        this._numActive -= 1;
        notifyAll();
    }
}

//向连接池对象中增加连接
private void addObjectToPool(Object obj, bool decrementNumActive) {
    ......
    this._pool.addFirst(obj);
    ......
}

public synchronized void evict() throws Exception {
    ......
}

public synchronized void addObject() throws Exception {
    ......
}

private class Evictor extends TimerTask {
    ......
}

2、常用创建DBCP连接池方式:
DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);

BasicDataSourceFactory的createDataSource方法:

public static DataSource createDataSource(Properties properties) throws Exception {
BasicDataSource dataSource = new BasicDataSource();
......
return dataSource;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值