Search in sources :

Example 1 with NormalConnection

use of com.pamirs.attach.plugin.common.datasource.normal.NormalConnection in project LinkAgent by shulieTech.

the class TomcatJdbcMediatorDataSource method getConnection.

@Override
public Connection getConnection() throws SQLException {
    if (Pradar.isClusterTest()) {
        try {
            init();
            TomcatJdbcConstants.dbConnectionKey = dbConnectionKey;
            TomcatJdbcConstants.dbType = dbType;
            TomcatJdbcConstants.useTable = useTable;
            if (useTable) {
                // 影子表
                if (dataSourceBusiness == null) {
                    throw new PressureMeasureError("Business dataSource is null.");
                }
                return new NormalConnection(dataSourceBusiness, dataSourceBusiness.getConnection(), dbConnectionKey, url, username, dbType, getMidType());
            } else {
                // 影子库
                if (dataSourcePerformanceTest == null) {
                    throw new PressureMeasureError("Performance dataSource is null.");
                }
                return new PressureConnection(dataSourceBusiness, dataSourcePerformanceTest.getConnection(), dataSourcePerformanceTest.getUrl(), dataSourcePerformanceTest.getUsername(), dbConnectionKey, dbType);
            }
        } catch (Throwable e) {
            ErrorReporter.Error error = ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0001").setMessage("数据源获取链接失败!" + ((Pradar.isClusterTest() ? "(压测流量)" : "") + ", url=" + (dataSourceBusiness == null ? null : dataSourceBusiness.getUrl()) + ", username=" + (dataSourceBusiness == null ? null : dataSourceBusiness.getUsername()))).setDetail("get connection failed by dbMediatorDataSource, url=" + (dataSourceBusiness == null ? null : dataSourceBusiness.getUrl()) + ", username=" + (dataSourceBusiness == null ? null : dataSourceBusiness.getUsername()) + "message: " + e.getMessage() + "\r\n" + printStackTrace(e));
            // error.closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS);
            error.report();
            throw new PressureMeasureError("get connection failed by dbMediatorDataSource. url=" + (dataSourceBusiness == null ? null : dataSourceBusiness.getUrl()) + ", username=" + (dataSourceBusiness == null ? null : dataSourceBusiness.getUsername()), e);
        }
    } else {
        String dbType = JdbcUtils.getDbType(dataSourceBusiness.getUrl(), JdbcUtils.getDriverClassName(dataSourceBusiness.getUrl()));
        return new BizConnection(dataSourceBusiness.getConnection(), dataSourceBusiness.getUrl(), dataSourceBusiness.getUsername(), dbType);
    }
}
Also used : PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) NormalConnection(com.pamirs.attach.plugin.common.datasource.normal.NormalConnection) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) BizConnection(com.pamirs.attach.plugin.common.datasource.biz.BizConnection) PressureConnection(com.pamirs.attach.plugin.common.datasource.pressure.PressureConnection)

Example 2 with NormalConnection

use of com.pamirs.attach.plugin.common.datasource.normal.NormalConnection in project LinkAgent by shulieTech.

the class TomcatJdbcMediatorDataSource method getConnection.

@Override
public Connection getConnection(String username, String password) throws SQLException {
    if (Pradar.isClusterTest()) {
        try {
            init();
            TomcatJdbcConstants.dbConnectionKey = dbConnectionKey;
            TomcatJdbcConstants.dbType = dbType;
            TomcatJdbcConstants.useTable = useTable;
            if (useTable) {
                // 影子表
                if (dataSourceBusiness == null) {
                    throw new PressureMeasureError("Business dataSource is null.");
                }
                return new NormalConnection(dataSourceBusiness, dataSourceBusiness.getConnection(username, password), dbConnectionKey, url, username, dbType, "other");
            } else {
                // 影子库
                if (dataSourcePerformanceTest == null) {
                    throw new PressureMeasureError("Performance dataSource is null.");
                }
                return new PressureConnection(dataSourceBusiness, dataSourcePerformanceTest.getConnection(username, password), url, username, dbConnectionKey, dbType);
            }
        } catch (Throwable e) {
            ErrorReporter.Error error = ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0001").setMessage("数据源获取链接失败!" + (Pradar.isClusterTest() ? "(压测流量)" : "")).setDetail("get connection failed by dbMediatorDataSource, message: " + e.getMessage() + "\r\n" + printStackTrace(e));
            // error.closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS);
            error.report();
            throw new PressureMeasureError("tomcat jdbc get connection failed by dbMediatorDataSource");
        }
    } else {
        String dbType = JdbcUtils.getDbType(dataSourceBusiness.getUrl(), JdbcUtils.getDriverClassName(dataSourceBusiness.getUrl()));
        return new BizConnection(dataSourceBusiness.getConnection(username, password), dataSourceBusiness.getUrl(), dataSourceBusiness.getUsername(), dbType);
    }
}
Also used : PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) NormalConnection(com.pamirs.attach.plugin.common.datasource.normal.NormalConnection) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) BizConnection(com.pamirs.attach.plugin.common.datasource.biz.BizConnection) PressureConnection(com.pamirs.attach.plugin.common.datasource.pressure.PressureConnection)

Aggregations

BizConnection (com.pamirs.attach.plugin.common.datasource.biz.BizConnection)2 NormalConnection (com.pamirs.attach.plugin.common.datasource.normal.NormalConnection)2 PressureConnection (com.pamirs.attach.plugin.common.datasource.pressure.PressureConnection)2 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)2