Search in sources :

Example 6 with DalResultSetExtractor

use of com.ctrip.platform.dal.dao.DalResultSetExtractor in project dal by ctripcorp.

the class HATest method testHAWithMarkdowns.

@Test
public void testHAWithMarkdowns() throws Exception {
    DalStatusManager.getDataSourceStatus("MySqlShard_1").setManualMarkdown(true);
    hints = new DalHints();
    Integer count = 0;
    try {
        count = database2.query(sql, new StatementParameters(), hints, new DalResultSetExtractor<Integer>() {

            @Override
            public Integer extract(ResultSet rs) throws SQLException {
                if (0 == markCount) {
                    markCount++;
                    mockRetryThrows(hints.getHA());
                }
                if (1 == markCount) {
                    markCount++;
                    mockFailOverThrow(hints.getHA());
                } else {
                    //Here fail over to master
                    while (rs.next()) {
                        return rs.getInt(1);
                    }
                }
                return 0;
            }
        });
    } catch (SQLException e) {
    }
    Assert.assertEquals(3, count == null ? 0 : count.intValue());
    DalStatusManager.getDataSourceStatus("MySqlShard_1").setManualMarkdown(false);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalResultSetExtractor(com.ctrip.platform.dal.dao.DalResultSetExtractor) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ResultSet(java.sql.ResultSet) DalQueryDaoMySqlTest(test.com.ctrip.platform.dal.dao.shard.DalQueryDaoMySqlTest) Test(org.junit.Test)

Example 7 with DalResultSetExtractor

use of com.ctrip.platform.dal.dao.DalResultSetExtractor in project dal by ctripcorp.

the class HATest method testTheSecondFailOverSuccess.

@Test
public void testTheSecondFailOverSuccess() throws SQLException {
    hints = new DalHints();
    Integer count = 0;
    try {
        count = database2.query(sql, new StatementParameters(), hints, new DalResultSetExtractor<Integer>() {

            @Override
            public Integer extract(ResultSet rs) throws SQLException {
                if (1 == markCount) {
                    markCount++;
                    while (rs.next()) {
                        return rs.getInt(1);
                    }
                } else {
                    markCount++;
                    mockFailOverThrow(hints.getHA());
                }
                return 0;
            }
        });
    } catch (SQLException e) {
    }
    Assert.assertEquals(3, count == null ? 0 : count.intValue());
    Assert.assertEquals(1, hints.getHA().getRetryCount());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalResultSetExtractor(com.ctrip.platform.dal.dao.DalResultSetExtractor) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ResultSet(java.sql.ResultSet) DalQueryDaoMySqlTest(test.com.ctrip.platform.dal.dao.shard.DalQueryDaoMySqlTest) Test(org.junit.Test)

Example 8 with DalResultSetExtractor

use of com.ctrip.platform.dal.dao.DalResultSetExtractor in project dal by ctripcorp.

the class HATest method testTheSecondRetrySuccess.

@Test
public void testTheSecondRetrySuccess() {
    hints = new DalHints();
    Integer count = 0;
    try {
        count = database.query(sql, new StatementParameters(), hints, new DalResultSetExtractor<Integer>() {

            @Override
            public Integer extract(ResultSet rs) throws SQLException {
                if (1 == markCount++) {
                    while (rs.next()) {
                        return rs.getInt(1);
                    }
                }
                mockRetryThrows(hints.getHA());
                return 0;
            }
        });
    } catch (SQLException e) {
    }
    Assert.assertEquals(3, count == null ? 0 : count.intValue());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalResultSetExtractor(com.ctrip.platform.dal.dao.DalResultSetExtractor) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ResultSet(java.sql.ResultSet) DalQueryDaoMySqlTest(test.com.ctrip.platform.dal.dao.shard.DalQueryDaoMySqlTest) Test(org.junit.Test)

Aggregations

DalResultSetExtractor (com.ctrip.platform.dal.dao.DalResultSetExtractor)8 ResultSet (java.sql.ResultSet)8 DalHints (com.ctrip.platform.dal.dao.DalHints)7 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)7 SQLException (java.sql.SQLException)7 Test (org.junit.Test)7 DalQueryDaoMySqlTest (test.com.ctrip.platform.dal.dao.shard.DalQueryDaoMySqlTest)7 HintsAwareExtractor (com.ctrip.platform.dal.dao.helper.HintsAwareExtractor)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1