Search in sources :

Example 16 with DalHints

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

the class MySqlHelperTest method replaceTest.

@Test
public void replaceTest() throws SQLException {
    int count = 3;
    List<Person> persons = new ArrayList<>();
    for (int i = 1; i <= count; i++) {
        Person pojo1 = new Person();
        pojo1.setID(i);
        pojo1.setName("forest" + i);
        pojo1.setBirth(new Timestamp(System.currentTimeMillis()));
        persons.add(pojo1);
    }
    client.delete(hints, persons);
    KeyHolder holder = new KeyHolder();
    helper.replace(holder, hints, persons);
    List<Map<String, Object>> generateKeys = holder.getKeyList();
    assertTrue(generateKeys.size() == 3);
    assertTrue(generateKeys.get(0).containsKey("GENERATED_KEY"));
    persons.get(1).setName("jack1");
    helper.replace(holder, new DalHints(), persons);
    Person rep = client.queryByPk(2, hints);
    assertTrue(rep.getName().equals("jack1"));
    client.delete(hints, persons);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Timestamp(java.sql.Timestamp) Map(java.util.Map) Test(org.junit.Test)

Example 17 with DalHints

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

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

the class ShardColModShardStrategyTest method testLocateDbShardByShardValue.

@Test
public void testLocateDbShardByShardValue() 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);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(0)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(1)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(2)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(3)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(100000000000L)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(100000000001L)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(100000000002L)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(100000000003L)));
}
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) Test(org.junit.Test)

Example 19 with DalHints

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

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

the class ShardColModShardStrategyTest method testLocateTableShardByTableShardValue.

@Test
public void testLocateTableShardByTableShardValue() 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");
    settings.put(ShardColModShardStrategy.SEPARATOR, "_");
    strategy.initialize(settings);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("0")));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("1")));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("2")));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("3")));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("4")));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("5")));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("6")));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("7")));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(0)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(1)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(2)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(3)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(4)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(5)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(6)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(7)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(100000000004L)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(100000000005L)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(100000000006L)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(100000000007L)));
}
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) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)621 Test (org.junit.Test)473 SQLException (java.sql.SQLException)278 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)245 ArrayList (java.util.ArrayList)113 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)57 Future (java.util.concurrent.Future)32 KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)27 DataBase (com.ctrip.platform.dal.dao.configure.DataBase)26 DatabaseSelector (com.ctrip.platform.dal.dao.configure.DatabaseSelector)26 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)25 AfterClass (org.junit.AfterClass)23 BeforeClass (org.junit.BeforeClass)23 List (java.util.List)22 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)20 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)18 After (org.junit.After)17 Before (org.junit.Before)17 HashMap (java.util.HashMap)16 Map (java.util.Map)16