Search in sources :

Example 1 with DatabaseCategory

use of com.ctrip.platform.dal.common.enums.DatabaseCategory in project dal by ctripcorp.

the class TimeoutDetectorTest method isTimeOutExceptionTest.

@Test
public void isTimeOutExceptionTest() {
    DatabaseCategory ct = DatabaseCategory.MySql;
    SQLException e = this.mockTimeoutException(ct);
    ErrorContext ctx = new ErrorContext(dbName, ct, 10000, e);
    Assert.assertTrue(TimeoutDetector.isTimeOutException(ctx));
    ct = DatabaseCategory.SqlServer;
    e = this.mockTimeoutException(ct);
    ctx = new ErrorContext(dbName, ct, 10000, e);
    Assert.assertTrue(TimeoutDetector.isTimeOutException(ctx));
    ct = DatabaseCategory.SqlServer;
    e = new SQLException("查询超时");
    ctx = new ErrorContext(dbName, ct, 10000, e);
    Assert.assertTrue(TimeoutDetector.isTimeOutException(ctx));
    ct = DatabaseCategory.MySql;
    e = this.mockNotTimeoutException();
    ctx = new ErrorContext(dbName, ct, 10000, e);
    Assert.assertFalse(TimeoutDetector.isTimeOutException(ctx));
}
Also used : DatabaseCategory(com.ctrip.platform.dal.common.enums.DatabaseCategory) SQLException(java.sql.SQLException) ErrorContext(com.ctrip.platform.dal.dao.markdown.ErrorContext) Test(org.junit.Test)

Example 2 with DatabaseCategory

use of com.ctrip.platform.dal.common.enums.DatabaseCategory in project dal by ctripcorp.

the class DataSourceSwitchChecker method getDBServerName.

public static String getDBServerName(Connection conn, DataSourceConfigure configure) {
    String executeSql;
    int columnIndex;
    String serverName = null;
    DatabaseCategory databaseCategory = configure.getDatabaseCategory();
    if (databaseCategory.equals(DatabaseCategory.MySql)) {
        executeSql = MYSQL_SQL;
        columnIndex = 2;
    } else if (databaseCategory.equals(DatabaseCategory.SqlServer)) {
        executeSql = SQLSERVER_SQL;
        columnIndex = 1;
    } else {
        return null;
    }
    Statement stmt = null;
    ResultSet rs = null;
    long startTime = System.currentTimeMillis();
    try {
        stmt = conn.createStatement();
        stmt.setQueryTimeout(defaultCheckerTimeout);
        rs = stmt.executeQuery(executeSql);
        while (rs.next()) {
            serverName = rs.getString(columnIndex);
        }
    } catch (Exception e) {
        LOGGER.error("check db server name failed! ", e);
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        } catch (Exception ex) {
        // ignore
        }
    }
    String logName = configure.getName() != null ? configure.getName() : ConnectionUtils.getConnectionUrl(conn);
    LOGGER.logTransaction(DalLogTypes.DAL_DATASOURCE, String.format(DATASOURCE_SWITCH_DATASOURCE, logName), serverName, startTime);
    return serverName;
}
Also used : DatabaseCategory(com.ctrip.platform.dal.common.enums.DatabaseCategory) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 3 with DatabaseCategory

use of com.ctrip.platform.dal.common.enums.DatabaseCategory in project dal by ctripcorp.

the class DalConnection method isDiscardException.

private boolean isDiscardException(Throwable t) {
    Throwable t1 = t;
    while (t1 instanceof DalException) {
        t1 = t1.getCause();
    }
    while (t1 != null && !(t1 instanceof SQLException)) {
        t1 = t1.getCause();
    }
    if (t1 == null)
        return false;
    DatabaseCategory dbCategory = dataSource.getDatabaseCategory();
    SQLException se = (SQLException) t1;
    if (dbCategory.isSpecificException(se))
        return true;
    return isDiscardException(se.getNextException());
}
Also used : DatabaseCategory(com.ctrip.platform.dal.common.enums.DatabaseCategory) DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 4 with DatabaseCategory

use of com.ctrip.platform.dal.common.enums.DatabaseCategory in project dal by ctripcorp.

the class TimeoutDetectorTest method countBaseLineMatchTest.

@Test
public void countBaseLineMatchTest() {
    TimeoutDetector detector = new TimeoutDetector();
    DalStatusManager.getMarkdownStatus().setEnableAutoMarkdown(true);
    DalStatusManager.getTimeoutMarkdown().setEnabled(true);
    DalStatusManager.getTimeoutMarkdown().setErrorCountThreshold(5);
    for (int i = 0; i < 10; i++) {
        SQLException e = this.mockNotTimeoutException();
        DatabaseCategory ct = DatabaseCategory.MySql;
        if (i % 2 == 0) {
            ct = random.nextBoolean() ? DatabaseCategory.MySql : DatabaseCategory.SqlServer;
            e = this.mockTimeoutException(ct);
        }
        ErrorContext ctx = new ErrorContext(dbName, ct, 1000, e);
        detector.detect(ctx);
    }
    Assert.assertTrue(MarkdownManager.isMarkdown(dbName));
}
Also used : DatabaseCategory(com.ctrip.platform.dal.common.enums.DatabaseCategory) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 5 with DatabaseCategory

use of com.ctrip.platform.dal.common.enums.DatabaseCategory in project dal by ctripcorp.

the class TimeoutDetectorTest method countBaseLineMatchButOverdueTest.

@Test
public void countBaseLineMatchButOverdueTest() {
    TimeoutDetector detector = new TimeoutDetector();
    DalStatusManager.getTimeoutMarkdown().setEnabled(true);
    DalStatusManager.getTimeoutMarkdown().setErrorCountThreshold(5);
    DalStatusManager.getTimeoutMarkdown().setSamplingDuration(1);
    for (int i = 0; i < 10; i++) {
        SQLException e = this.mockNotTimeoutException();
        DatabaseCategory ct = DatabaseCategory.MySql;
        if (i % 2 == 0) {
            ct = random.nextBoolean() ? DatabaseCategory.MySql : DatabaseCategory.SqlServer;
            e = this.mockTimeoutException(ct);
        }
        if (i == 4) {
            try {
                Thread.sleep(1100);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        ErrorContext ctx = new ErrorContext(dbName, ct, 1000, e);
        detector.detect(ctx);
    }
    Assert.assertFalse(MarkdownManager.isMarkdown(dbName));
}
Also used : DatabaseCategory(com.ctrip.platform.dal.common.enums.DatabaseCategory) SQLException(java.sql.SQLException) ErrorContext(com.ctrip.platform.dal.dao.markdown.ErrorContext) TimeoutDetector(com.ctrip.platform.dal.dao.markdown.TimeoutDetector) Test(org.junit.Test)

Aggregations

DatabaseCategory (com.ctrip.platform.dal.common.enums.DatabaseCategory)10 SQLException (java.sql.SQLException)8 Test (org.junit.Test)8 ErrorContext (com.ctrip.platform.dal.dao.markdown.ErrorContext)4 TimeoutDetector (com.ctrip.platform.dal.dao.markdown.TimeoutDetector)3 DalException (com.ctrip.platform.dal.exceptions.DalException)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1