Search in sources :

Example 1 with DalCommand

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

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

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

the class DalDirectClientTestStub method executeTestWithMultipleCommandsAllSuccessed.

/**
	 * Test execute multiple commands and all successes.
	 * @throws SQLException
	 */
@Test
public void executeTestWithMultipleCommandsAllSuccessed() throws SQLException {
    final DalHints hints = new DalHints();
    List<DalCommand> commands = new ArrayList<DalCommand>();
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return true;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 2";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return true;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 3";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return true;
        }
    });
    client.execute(commands, hints);
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(0, models.size());
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalHints(com.ctrip.platform.dal.dao.DalHints) DalClient(com.ctrip.platform.dal.dao.DalClient) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with DalCommand

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

the class DalShardingHelperTest method testIsAlreadySharded.

@Test
public void testIsAlreadySharded() {
    DalClient client;
    try {
        // No shard enabled
        assertTrue(DalShardingHelper.isAlreadySharded("HA_Test_1", null, null));
    } catch (SQLException e) {
        e.printStackTrace();
    }
    //is in transaction
    client = DalClientFactory.getClient("dao_test_sqlsvr_tableShard");
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                assertTrue(DalShardingHelper.isAlreadySharded("dao_test_sqlsvr_tableShard", "dal_client_test", null));
                return false;
            }
        }, new DalHints().inShard("0"));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Test shard by DB
    try {
        assertFalse(DalShardingHelper.isAlreadySharded("dao_test_mod", null, new DalHints()));
        assertTrue(DalShardingHelper.isAlreadySharded("dao_test_mod", null, new DalHints().inShard("0")));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Test shard by table
    try {
        assertFalse(DalShardingHelper.isAlreadySharded("dao_test_sqlsvr_tableShard", "dal_client_test", new DalHints()));
        assertTrue(DalShardingHelper.isAlreadySharded("dao_test_sqlsvr_tableShard", "dal_client_test", new DalHints().inTableShard("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) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 5 with DalCommand

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

the class DalShardingHelperTest method testShuffle.

@Test
public void testShuffle() {
    //;columns=id;mod=2
    final String logicDbName = "dao_test_mod";
    final List<Map<String, ?>> daoPojos = new ArrayList<>();
    DalClient client;
    String shardId;
    Map<String, Object> pojo = new HashMap<>();
    pojo.put("id", 0);
    daoPojos.add(pojo);
    pojo = new HashMap<>();
    pojo.put("id", 1);
    daoPojos.add(pojo);
    // Test by pojos
    try {
        shardId = null;
        Map<String, Map<Integer, Map<String, ?>>> shuffled = DalShardingHelper.shuffle(logicDbName, shardId, daoPojos);
        assertEquals(2, shuffled.size());
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Test preset shardid
    try {
        shardId = "0";
        Map<String, Map<Integer, Map<String, ?>>> shuffled = DalShardingHelper.shuffle(logicDbName, shardId, daoPojos);
        assertEquals(1, shuffled.size());
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // same shard
    client = DalClientFactory.getClient(logicDbName);
    try {
        assertFalse(DalTransactionManager.isInTransaction());
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.shuffle(logicDbName, "0", daoPojos);
                return false;
            }
        }, new DalHints().inShard("0"));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Detect in different shard with two shard ids
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.shuffle(logicDbName, null, daoPojos);
                return false;
            }
        }, new DalHints().inShard("0"));
        fail();
    } catch (SQLException e) {
    }
    // Detect in different shard with one shard ids
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

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

Aggregations

DalClient (com.ctrip.platform.dal.dao.DalClient)11 DalCommand (com.ctrip.platform.dal.dao.DalCommand)11 DalHints (com.ctrip.platform.dal.dao.DalHints)10 SQLException (java.sql.SQLException)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)6 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)3 DalScalarExtractor (com.ctrip.platform.dal.dao.helper.DalScalarExtractor)2 ConnectionAction (com.ctrip.platform.dal.dao.client.ConnectionAction)1 DalTransactionListener (com.ctrip.platform.dal.dao.client.DalTransactionListener)1 DalTransactionManager (com.ctrip.platform.dal.dao.client.DalTransactionManager)1 ResultSet (java.sql.ResultSet)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1