Search in sources :

Example 1 with DalResultSetExtractor

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

the class HATest method testFirstRetrySecondeFailOver.

@Test
public void testFirstRetrySecondeFailOver() {
    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 the third slave
                    while (rs.next()) {
                        return rs.getInt(1);
                    }
                }
                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)

Example 2 with DalResultSetExtractor

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

the class DalDirectClient method query.

@Override
public List<?> query(String sql, StatementParameters parameters, final DalHints hints, final List<DalResultSetExtractor<?>> extractors) throws SQLException {
    ConnectionAction<List<?>> action = new ConnectionAction<List<?>>() {

        @Override
        public List<?> execute() throws Exception {
            conn = getConnection(hints, this);
            preparedStatement = createPreparedStatement(conn, sql, parameters, hints);
            List<Object> result = new ArrayList<>();
            DalWatcher.beginExecute();
            preparedStatement.execute();
            for (DalResultSetExtractor<?> extractor : extractors) {
                ResultSet resultSet = preparedStatement.getResultSet();
                Object partResult;
                if (extractor instanceof HintsAwareExtractor)
                    partResult = ((DalResultSetExtractor) ((HintsAwareExtractor) extractor).extractWith(hints)).extract(resultSet);
                else
                    partResult = extractor.extract(resultSet);
                result.add(partResult);
                preparedStatement.getMoreResults();
            }
            DalWatcher.endExectue();
            return result;
        }
    };
    action.populate(DalEventEnum.QUERY, sql, parameters);
    return doInConnection(action, hints);
}
Also used : HintsAwareExtractor(com.ctrip.platform.dal.dao.helper.HintsAwareExtractor) DalResultSetExtractor(com.ctrip.platform.dal.dao.DalResultSetExtractor) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with DalResultSetExtractor

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

the class HATest method testNotRetryNotFailOver.

@Test
public void testNotRetryNotFailOver() {
    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 {
                throw createException(-100);
            }
        });
    } catch (SQLException e) {
    }
    Assert.assertEquals(0, 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 4 with DalResultSetExtractor

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

the class HATest method testAllRetryFailed.

@Test
public void testAllRetryFailed() {
    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 {
                mockRetryThrows(hints.getHA());
                return 0;
            }
        });
    } catch (SQLException e) {
    }
    Assert.assertEquals(0, count == null ? 0 : count.intValue());
    Assert.assertEquals(DalStatusManager.getHaStatus().getRetryCount(), 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 5 with DalResultSetExtractor

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

the class HATest method testAllFailOverFailed.

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

            @Override
            public Integer extract(ResultSet rs) throws SQLException {
                mockFailOverThrow(hints.getHA());
                return 0;
            }
        });
    } catch (SQLException e) {
    }
    Assert.assertEquals(0, count == null ? 0 : count.intValue());
    Assert.assertEquals(3, 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)

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