Druiddruid

Druid 监控实现原理

2022-02-14  本文已影响0人  晴天哥_王志

Druid监控功能

Connection

public abstract class DruidAbstractDataSource extends WrapperAdapter implements DruidAbstractDataSourceMBean, DataSource, DataSourceProxy, Serializable {

    public PhysicalConnectionInfo createPhysicalConnection() throws SQLException {
        String url = this.getUrl();
        Properties connectProperties = getConnectProperties();
        // 省略相关的代码
     
        try {
            // 创建物理链接
            conn = createPhysicalConnection(url, physicalConnectProperties);
            connectedNanos = System.nanoTime();

        } catch (SQLException ex) {
            throw ex;
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Error ex) {
            throw ex;
        } finally {
            long nano = System.nanoTime() - connectStartNanos;
            createTimespan += nano;
            creatingCountUpdater.decrementAndGet(this);
        }
        // 创建PhysicalConnectionInfo
        return new PhysicalConnectionInfo(conn, connectStartNanos, connectedNanos, initedNanos, validatedNanos, variables, globalVariables);
    }


    public Connection createPhysicalConnection(String url, Properties info) throws SQLException {
        Connection conn;
        if (getProxyFilters().size() == 0) {
            conn = getDriver().connect(url, info);
        } else {
            // 通过FilterChainImpl进行装饰
            conn = new FilterChainImpl(this).connection_connect(info);
        }

        createCountUpdater.incrementAndGet(this);

        return conn;
    }
public class FilterChainImpl implements FilterChain {

    public ConnectionProxy connection_connect(Properties info) throws SQLException {
        // 通过Filter进行封装
        if (this.pos < filterSize) {
            return nextFilter()
                    .connection_connect(this, info);
        }

        Driver driver = dataSource.getRawDriver();
        String url = dataSource.getRawJdbcUrl();

        Connection nativeConnection = driver.connect(url, info);

        if (nativeConnection == null) {
            return null;
        }

        return new ConnectionProxyImpl(dataSource, nativeConnection, info, dataSource.createConnectionId());
    }
}

Statement

public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionProxy {

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        FilterChainImpl chain = createChain();
        // 通过FilterChainImpl获取prepareStatement
        PreparedStatement stmt = chain.connection_prepareStatement(this, sql);
        recycleFilterChain(chain);
        return stmt;
    }
}


public class FilterChainImpl implements FilterChain {

    @Override
    public PreparedStatementProxy connection_prepareStatement(
            ConnectionProxy connection,
            String sql,
            int autoGeneratedKeys) throws SQLException
    {
        // 通过FilterChainImpl获取prepareStatement
        if (this.pos < filterSize) {
            return nextFilter()
                    .connection_prepareStatement(this, connection, sql, autoGeneratedKeys);
        }

        PreparedStatement statement = connection.getRawObject().prepareStatement(sql, autoGeneratedKeys);

        if (statement == null) {
            return null;
        }
        // 返回 PreparedStatementProxyImpl
        return new PreparedStatementProxyImpl(connection, statement, sql, dataSource.createStatementId());
    }
}

Filter

上一篇 下一篇

猜你喜欢

热点阅读