DBCP 源码初探

来源:互联网 发布:淘宝宝贝描述评分4.7 编辑:程序博客网 时间:2024/05/20 10:20

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);//初始化成员变量dataSourceprotected 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;......//下面几个属性拷贝自BasicDataSourceprivate 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,Serializableprivate 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;
}

原创粉丝点击