Search in sources :

Example 6 with StatementParameters

use of com.ctrip.platform.dal.dao.StatementParameters 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 7 with StatementParameters

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

the class ShardColModShardStrategyTest method testLocateDbShardByParameters.

@Test
public void testLocateDbShardByParameters() throws Exception {
    DalConfigure configure = DalConfigureFactory.load();
    ShardColModShardStrategy strategy = new ShardColModShardStrategy();
    Map<String, String> settings = new HashMap<String, String>();
    settings.put(ShardColModShardStrategy.COLUMNS, "id,id1");
    settings.put(ShardColModShardStrategy.MOD, "2");
    settings.put(ShardColModShardStrategy.TABLE_COLUMNS, "index,index1");
    settings.put(ShardColModShardStrategy.TABLE_MOD, "4");
    strategy.initialize(settings);
    StatementParameters parameters = null;
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.INTEGER, 0);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.INTEGER, 1);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.INTEGER, 2);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.INTEGER, 3);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.BIGINT, 100000000003L);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) HashMap(java.util.HashMap) ShardColModShardStrategy(com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 8 with StatementParameters

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

the class DalTabelDaoShardByTableSqlSvrTest method tearDownAfterClass.

@AfterClass
public static void tearDownAfterClass() throws Exception {
    DalHints hints = new DalHints();
    String[] sqls = null;
    //For Sql Server
    hints = new DalHints();
    StatementParameters parameters = new StatementParameters();
    for (int i = 0; i < mod; i++) {
        sqls = new String[] { String.format(DROP_TABLE_SQL_SQLSVR_TPL, i, i) };
        for (int j = 0; j < sqls.length; j++) {
            clientSqlSvr.update(sqls[j], parameters, hints);
        }
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) AfterClass(org.junit.AfterClass)

Example 9 with StatementParameters

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

the class DalTabelDaoShardByTableSqlSvrTest method tearDown.

@After
public void tearDown() throws Exception {
    String sql = "DELETE FROM " + TABLE_NAME;
    StatementParameters parameters = new StatementParameters();
    DalHints hints = new DalHints();
    sql = "DELETE FROM " + TABLE_NAME;
    parameters = new StatementParameters();
    hints = new DalHints();
    try {
        for (int i = 0; i < mod; i++) {
            clientSqlSvr.update(sql + "_" + i, parameters, hints);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) After(org.junit.After)

Example 10 with StatementParameters

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

the class DalTabelDaoShardByTableSqlSvrTest method setUpBeforeClass.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    DalClientFactory.initClientFactory();
    clientSqlSvr = DalClientFactory.getClient(DATABASE_NAME_SQLSVR);
    DalHints hints = new DalHints();
    String[] sqls = null;
    // For SQL server
    hints = new DalHints();
    StatementParameters parameters = new StatementParameters();
    for (int i = 0; i < mod; i++) {
        sqls = new String[] { String.format(DROP_TABLE_SQL_SQLSVR_TPL, i, i), String.format(CREATE_TABLE_SQL_SQLSVR_TPL, i) };
        for (int j = 0; j < sqls.length; j++) {
            clientSqlSvr.update(sqls[j], parameters, hints);
        }
    }
    clientSqlSvr.update(DROP_TABLE_SQL_SQLSVR_TPL_1, parameters, hints);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) BeforeClass(org.junit.BeforeClass)

Aggregations

StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)276 DalHints (com.ctrip.platform.dal.dao.DalHints)245 Test (org.junit.Test)193 SQLException (java.sql.SQLException)97 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)40 ArrayList (java.util.ArrayList)32 After (org.junit.After)17 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)16 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 ResultSet (java.sql.ResultSet)10 DalClient (com.ctrip.platform.dal.dao.DalClient)9 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)8 DalQueryDao (com.ctrip.platform.dal.dao.DalQueryDao)7 DalResultSetExtractor (com.ctrip.platform.dal.dao.DalResultSetExtractor)7 Map (java.util.Map)7 AfterClass (org.junit.AfterClass)7 BeforeClass (org.junit.BeforeClass)7 DalCommand (com.ctrip.platform.dal.dao.DalCommand)6 HashSet (java.util.HashSet)6