use of com.ctrip.platform.dal.dao.DalClient 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.DalClient 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.DalClient in project dal by ctripcorp.
the class DalColumnMapRowMapperTest method testDalColumnMapRowMapperEmpty.
@Test
public void testDalColumnMapRowMapperEmpty() {
try {
StatementParameters parameters = new StatementParameters();
DalClient client = DalClientFactory.getClient(DATABASE_NAME);
List<Map<String, Object>> result1 = client.query(sqlNoResult, parameters, new DalHints(), new DalRowMapperExtractor<Map<String, Object>>(new DalColumnMapRowMapper()));
assertEquals(0, result1.size());
DalQueryDao dao = new DalQueryDao(DATABASE_NAME);
List<Map<String, Object>> result = dao.query(sqlNoResult, parameters, hints, new DalColumnMapRowMapper());
assertEquals(0, result.size());
} catch (Exception e) {
fail();
}
}
use of com.ctrip.platform.dal.dao.DalClient 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.DalClient in project dal by ctripcorp.
the class MarkdownAndUpIntergration method testQuery.
private void testQuery(String logicName) throws SQLException {
DalClient client = DalClientFactory.getClient(logicName);
client.query("SELECT 1", new StatementParameters(), new DalHints(), new DalResultSetExtractor<Integer>() {
@Override
public Integer extract(ResultSet rs) throws SQLException {
return null;
}
});
}
Aggregations