(二)atomikos 源码初探

atomikos最重要的入口是AtomikosDataSourceBean,我们就从这个入口着手进行分析。


public class AtomikosDataSourceBean extends com.atomikos.jdbc.AtomikosDataSourceBean implements BeanNameAware, InitializingBean, DisposableBean {
    private String beanName;

    public AtomikosDataSourceBean() {
    }

    public void setBeanName(String name) {
        this.beanName = name;
    }

    //每个版本的实现上有不同的差异,在4.0.6中init()方法是在获取连接时候调用的
    public void afterPropertiesSet() throws Exception {
        if (!StringUtils.hasLength(this.getUniqueResourceName())) {
            this.setUniqueResourceName(this.beanName);
        }
        //调用父类的初始化方法进行初始化
        this.init();
    }

    public void destroy() throws Exception {
        this.close();
    }
}

AtomikosDataSourceBean实现了spring的InitializingBean,在bean初始化时,默认会调用afterPropertiesSet进行初始化。

AtomikosDataSourceBean的类的继承链条

AtomikosDataSourceBean->com.atomikos.jdbc.AtomikosDataSourceBean->com.atomikos.jdbc.AbstractDataSourceBean

this.init();这句代码调用的是AbstractDataSourceBean的init方法。

AbstractDataSourceBean-> init()

public synchronized void init() throws AtomikosSQLException {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.logInfo(this + ": init...");
        }

        if (this.connectionPool == null) {
            if (this.maxPoolSize < 1) {
                this.throwAtomikosSQLException("Property 'maxPoolSize' must be greater than 0, was: " + this.maxPoolSize);
            }

            if (this.minPoolSize < 0 || this.minPoolSize > this.maxPoolSize) {
                this.throwAtomikosSQLException("Property 'minPoolSize' must be at least 0 and at most maxPoolSize, was: " + this.minPoolSize);
            }

            if (this.getUniqueResourceName() == null) {
                this.throwAtomikosSQLException("Property 'uniqueResourceName' cannot be null");
            }

            if (this.getMinPoolSize() == 1) {
                LOGGER.logWarning(this + ": poolSize equals default - this may cause performance problems!");
            }

            try {
                //调用com.atomikos.jdbc.AtomikosDataSourceBean的方法获取ConnectionFactory
                ConnectionFactory cf = this.doInit();
                //根据连接工厂创建对应的连接池
                if (this.enableConcurrentConnectionValidation) {
                    this.connectionPool = new ConnectionPoolWithConcurrentValidation(cf, this);
                } else {
                    if (this.getTestQuery() != null) {
                        LOGGER.logWarning(this + ": testQuery set - pool may be slower / you might want to consider setting maxLifetime instead...");
                    }

                    this.connectionPool = new ConnectionPoolWithSynchronizedValidation(cf, this);
                }

                this.getReference();
            } catch (AtomikosSQLException var3) {
                throw var3;
            } catch (Exception var4) {
                String msg = "Cannot initialize AtomikosDataSourceBean";
                AtomikosSQLException.throwAtomikosSQLException(msg, var4);
            }

            if (LOGGER.isTraceEnabled()) {
                LOGGER.logTrace(this + ": init done.");
            }

        }
    }

com.atomikos.jdbc.AtomikosDataSourceBean的doInit方法

protected ConnectionFactory doInit() throws Exception {
        if (this.xaDataSource == null) {
            if (this.xaDataSourceClassName == null) {
                this.throwAtomikosSQLException("Property 'xaDataSourceClassName' cannot be null");
            }

            if (this.xaProperties == null) {
                this.throwAtomikosSQLException("Property 'xaProperties' cannot be null");
            }
        }

        if (LOGGER.isDebugEnabled()) {
            LOGGER.logInfo(this + ": initializing with [" + " xaDataSourceClassName=" + this.xaDataSourceClassName + "," + " uniqueResourceName=" + this.getUniqueResourceName() + "," + " maxPoolSize=" + this.getMaxPoolSize() + "," + " minPoolSize=" + this.getMinPoolSize() + "," + " borrowConnectionTimeout=" + this.getBorrowConnectionTimeout() + "," + " maxIdleTime=" + this.getMaxIdleTime() + "," + " reapTimeout=" + this.getReapTimeout() + "," + " maintenanceInterval=" + this.getMaintenanceInterval() + "," + " testQuery=" + this.getTestQuery() + "," + " xaProperties=" + this.printXaProperties() + "," + " loginTimeout=" + this.getLoginTimeout() + "," + " maxLifetime=" + this.getMaxLifetime() + "]");
        }

        if (this.xaDataSource == null) {
            try {
                Class<XADataSource> xadsClass = ClassLoadingHelper.loadClass(this.getXaDataSourceClassName());
                this.xaDataSource = (XADataSource)xadsClass.newInstance();
            } catch (ClassNotFoundException var3) {
                AtomikosSQLException.throwAtomikosSQLException("The class '" + this.getXaDataSourceClassName() + "' specified by property 'xaDataSourceClassName' could not be found in the classpath. Please make sure the spelling is correct, and that the required jar(s) are in the classpath.", var3);
            } catch (ClassCastException var4) {
                AtomikosSQLException.throwAtomikosSQLException("The class '" + this.getXaDataSourceClassName() + "' specified by property 'xaDataSourceClassName' does not implement the required interface javax.jdbc.XADataSource. Please make sure the spelling is correct, and check your JDBC driver vendor's documentation.");
            }

            this.xaDataSource.setLoginTimeout(this.getLoginTimeout());
            this.xaDataSource.setLogWriter(this.getLogWriter());
            PropertyUtils.setProperties(this.xaDataSource, this.xaProperties);
        }

        JdbcTransactionalResource tr = new JdbcTransactionalResource(this.getUniqueResourceName(), this.xaDataSource);
        ConnectionFactory cf = new AtomikosXAConnectionFactory(this.xaDataSource, tr, this);
        Configuration.addResource(tr);
        EventPublisher.publish(new XAResourceDetectedEvent(this.xaDataSourceClassName, this.xaProperties, ResourceType.JDBC));
        return cf;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值