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();
}
}
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();
}
}
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());
}
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();
}
}
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) {
}
}
Aggregations