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