(三)以jdbc步骤来分析atomikos 源码-获取连接

com.atomikos.jdbc.AbstractDataSourceBean#getConnection()

public Connection getConnection() throws SQLException {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.logDebug(this + ": getConnection()...");
        }

        Connection connection = null;
        this.init();

        try {
            //比较关键的一行代码,调用的com.atomikos.datasource.pool.ConnectionPoolWithSynchronizedValidation方法
            //该方法最终拿到的就是AtomikosConnectionProxy
            connection = (Connection)this.connectionPool.borrowConnection();
        } catch (CreateConnectionException var3) {
            this.throwAtomikosSQLException("Failed to grow the connection pool", var3);
        } catch (PoolExhaustedException var4) {
            this.throwAtomikosSQLException("Connection pool exhausted - try increasing 'maxPoolSize' and/or 'borrowConnectionTimeout' on the DataSourceBean.");
        } catch (ConnectionPoolException var5) {
            this.throwAtomikosSQLException("Error borrowing connection", var5);
        }

        if (LOGGER.isTraceEnabled()) {
            LOGGER.logTrace(this + ": returning " + connection);
        }

        return connection;
    }

 AtomikosConnectionProxy类分析

com.atomikos.jdbc.AtomikosConnectionProxy#invoke

public Object invoke(Object proxy, Method method, Object[] args) throws SQLException {
        String methodName = method.getName();
        if (methodName.equals("getInvocationHandler")) {
            return this;
        } else if (methodName.equals("reap")) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.logDebug(this + ": reaping pending connection...");
            }

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

            return null;
        } else if (methodName.equals("isClosed")) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.logDebug(this + ": isClosed()...");
            }

            Object ret = this.closed;
            if (LOGGER.isTraceEnabled()) {
                LOGGER.logTrace(this + ": isClosed() returning " + ret);
            }

            return ret;
        } else if (this.closed && !this.methodAllowedAfterClose(method)) {
            String msg;
            if (this.reaped) {
                msg = "Connection has been reaped - calling " + methodName + " is no longer allowed! Increase reapTimeout to avoid this problem.";
                AtomikosSQLException.throwAtomikosSQLException(msg);
            } else {
                msg = "Connection was already closed - calling " + methodName + " is no longer allowed!";
                AtomikosSQLException.throwAtomikosSQLException(msg);
            }

            return null;
        } else {
            //在这里,获取连接时,第一步会走这里,这一步会去根据当前线程创建CompositeTransaction放到本地线程里面
            if (this.isEnlistedInGlobalTransaction()) {
                if (XA_INCOMPATIBLE_METHODS.contains(methodName)) {
                    AtomikosSQLException.throwAtomikosSQLException("Cannot call method '" + methodName + "' while a global transaction is running");
                }

                if (methodName.equals("setAutoCommit") && args[0].equals(Boolean.TRUE)) {
                    AtomikosSQLException.throwAtomikosSQLException("Cannot call 'setAutoCommit(true)' while a global transaction is running");
                }

                if (methodName.equals("getAutoCommit")) {
                    return Boolean.FALSE;
                }
            }

            if (ENLISTMENT_METHODS.contains(methodName)) {
                try {
                    this.enlist();
                } catch (Exception var8) {
                    this.sessionHandleState.notifySessionErrorOccurred();
                    JdbcConnectionProxyHelper.convertProxyError(var8, "Error enlisting in transaction - connection might be broken? Please check the logs for more information...");
                }
            }

            Object ret = null;
            if (CLOSE_METHODS.contains(methodName) && args == null) {
                this.close();
                return null;
            } else {
                try {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.logDebug(this + ": calling " + this.formatCallDetails(method, args) + "...");
                    }
                    //com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl去创建一个连接返回
                    ret = method.invoke(this.delegate, args);
                } catch (Exception var7) {
                    this.sessionHandleState.notifySessionErrorOccurred();
                    JdbcConnectionProxyHelper.convertProxyError(var7, "Error delegating '" + methodName + "' call");
                }

                if (LOGGER.isTraceEnabled()) {
                    LOGGER.logTrace(this + ": " + methodName + " returning " + ret);
                }

                if (ret instanceof Statement) {
                    Statement s = (Statement)ret;
                    this.addStatement(s);
                }

                return ret;
            }
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值