Search in sources :

Example 1 with DalHints

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

the class DalShardingHelperTest method testDetectDistributedTransactionStringDalHintsListOfMapOfStringQ.

//@Test
public void testDetectDistributedTransactionStringDalHintsListOfMapOfStringQ() {
    //;columns=id;mod=2
    final String logicDbName = "dao_test_mod";
    final List<Map<String, ?>> daoPojos = new ArrayList<>();
    DalClient client;
    String shardId;
    Set<String> shardIds = new HashSet<>();
    // Not in transaction
    try {
        shardIds.add("1");
        shardIds.add("0");
        DalShardingHelper.detectDistributedTransaction(shardIds);
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Shard ids is null
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(null);
                return false;
            }
        }, new DalHints().inShard("0"));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // More than 1 shards
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                Set<String> shardIds = new HashSet<>();
                shardIds.add("1");
                shardIds.add("0");
                DalShardingHelper.detectDistributedTransaction(shardIds);
                return false;
            }
        }, new DalHints().inShard("0"));
        fail();
    } catch (SQLException e) {
    }
    // Not same shard
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                Set<String> shardIds = new HashSet<>();
                shardIds.add("1");
                DalShardingHelper.detectDistributedTransaction(shardIds);
                return false;
            }
        }, new DalHints().inShard("0"));
        fail();
    } catch (SQLException e) {
    }
    // Not same shard
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                Set<String> shardIds = new HashSet<>();
                shardIds.add("0");
                DalShardingHelper.detectDistributedTransaction(shardIds);
                return false;
            }
        }, new DalHints().inShard("0"));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) Set(java.util.Set) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with DalHints

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

the class DalShardingHelperTest method testLocateTableShardId.

@Test
public void testLocateTableShardId() {
    //tableColumns=index,tableIndex;tableMod=4;separator=_;
    StatementParameters parameters = null;
    ;
    Map<String, Object> fields = null;
    try {
        // Preset
        assertEquals("0", DalShardingHelper.locateTableShardId("dao_test_sqlsvr_tableShard", new DalHints().inTableShard("0"), parameters, fields));
        // parameter
        parameters = new StatementParameters();
        parameters.set(1, "index", java.sql.Types.INTEGER, 1);
        assertEquals("1", DalShardingHelper.locateTableShardId("dao_test_sqlsvr_tableShard", new DalHints(), parameters, fields));
        //Fields
        parameters = null;
        fields = new HashMap<String, Object>();
        fields.put("index", 1);
        assertEquals("1", DalShardingHelper.locateTableShardId("dao_test_sqlsvr_tableShard", new DalHints(), parameters, fields));
    } catch (SQLException e) {
        e.printStackTrace();
        fail("Not yet implemented");
    }
    // Cannot locate
    try {
        // parameter
        parameters = new StatementParameters();
        parameters.set(1, "id", java.sql.Types.INTEGER, 1);
        fields = new HashMap<String, Object>();
        fields.put("ab", 1);
        assertEquals("1", DalShardingHelper.locateTableShardId("dao_test_sqlsvr_tableShard", new DalHints(), parameters, fields));
        fail();
    } catch (SQLException e) {
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 3 with DalHints

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

the class DalShardingHelperTest method testDetectDistributedTransactionSetOfString.

@Test
public void testDetectDistributedTransactionSetOfString() {
    DalClient client;
    // Not a shard enabled DB
    try {
        DalShardingHelper.detectDistributedTransaction("HA_Test_1", null, null);
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    //;columns=id;mod=2
    final String logicDbName = "dao_test_mod";
    // Not in transaction
    try {
        DalShardingHelper.detectDistributedTransaction(logicDbName, null, null);
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Shard is not same
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(logicDbName, new DalHints().inShard(0), null);
                return false;
            }
        }, new DalHints().inShard(1));
        fail();
    } catch (SQLException e) {
    }
    // Shard is same
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(logicDbName, new DalHints().inShard(1), null);
                return false;
            }
        }, new DalHints().inShard(1));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // can not decide and not same
    final List<Map<String, ?>> daoPojos = new ArrayList<>();
    Map<String, Object> pojo = new HashMap<>();
    pojo.put("id", 0);
    daoPojos.add(pojo);
    pojo = new HashMap<>();
    pojo.put("id", 1);
    daoPojos.add(pojo);
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(logicDbName, new DalHints(), daoPojos);
                return false;
            }
        }, new DalHints().inShard(1));
        fail();
    } catch (SQLException e) {
    }
    // can not decide and same
    daoPojos.clear();
    pojo = new HashMap<>();
    pojo.put("id", 1);
    daoPojos.add(pojo);
    pojo = new HashMap<>();
    pojo.put("id", 1);
    daoPojos.add(pojo);
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(logicDbName, new DalHints(), daoPojos);
                return false;
            }
        }, new DalHints().inShard(1));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with DalHints

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

the class DalTransactionManagerTest method testRegister.

@Test
public void testRegister() {
    final DalHints hints = new DalHints();
    final DalTransactionListener testListener = new DalTransactionListener() {

        @Override
        public void beforeCommit() {
        }

        @Override
        public void beforeRollback() {
        }

        @Override
        public void afterCommit() {
        }

        @Override
        public void afterRollback() {
        }
    };
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        DalTransactionManager.register(testListener);
        fail();
    } catch (Exception e) {
    }
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        ConnectionAction<?> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                DalTransactionManager.register(testListener);
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInTransaction(action, hints);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalTransactionListener(com.ctrip.platform.dal.dao.client.DalTransactionListener) DalHints(com.ctrip.platform.dal.dao.DalHints) DalTransactionManager(com.ctrip.platform.dal.dao.client.DalTransactionManager) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 5 with DalHints

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

the class PartialQueryQueryDaoTest method testAllowPartial.

@Test
public void testAllowPartial() throws Exception {
    // Test value here
    String name = "Test";
    List<Integer> cityIds = new ArrayList<>();
    cityIds.add(1);
    cityIds.add(2);
    cityIds.add(3);
    DalHints hints = new DalHints();
    FreeEntityMismatchPojo ret;
    try {
        hints = new DalHints();
        ret = findFreeFirstMismatch(name, cityIds, hints.inShard(1));
        fail();
    } catch (DalException e) {
        Assert.assertEquals(ErrorCode.ResultMappingError.getCode(), e.getErrorCode());
    }
    hints = new DalHints().allowPartial();
    ret = findFreeFirstMismatch(name, cityIds, hints.inShard(1));
    assertNotNull(ret);
    hints = new DalHints().allowPartial();
    ret = findFreeFirstMismatch(name, cityIds, hints.inAllShards());
    assertNotNull(ret);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) 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