use of com.ctrip.platform.dal.exceptions.DalException 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);
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class PartialQueryTableDaoUnitTest method testFindBySelectedField.
@Test
public void testFindBySelectedField() throws Exception {
DalTableDao<Person> client = new DalTableDao<>(new DalDefaultJpaParser<>(Person.class));
List<Integer> peopleIds = new ArrayList<>();
peopleIds.add(1);
peopleIds.add(2);
peopleIds.add(3);
List<Integer> cityIds = new ArrayList<>();
cityIds.add(1);
cityIds.add(2);
cityIds.add(3);
SelectSqlBuilder builder = new SelectSqlBuilder();
builder.select("DataChange_LastTime", "CityID", "Name", "ProvinceID");
builder.in("PeopleID", peopleIds, Types.INTEGER, false);
builder.and();
builder.in("CityID", cityIds, Types.INTEGER, false);
try {
List<Person> ret = client.query(builder, new DalHints().inAllShards().inTableShard(1));
Assert.assertNull(ret.get(0).getCountryID());
Assert.assertNull(ret.get(0).getPeopleID());
} catch (DalException e) {
Assert.fail();
}
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class DalConnectionManager method getNewConnection.
public DalConnection getNewConnection(DalHints hints, boolean useMaster, DalEventEnum operation) throws SQLException {
DalConnection connHolder = null;
String realDbName = logicDbName;
try {
if (DalStatusManager.getDatabaseSetStatus(logicDbName).isMarkdown())
throw new DalException(ErrorCode.MarkdownLogicDb, logicDbName);
boolean isMaster = hints.is(DalHintEnum.masterOnly) || useMaster;
boolean isSelect = operation == DalEventEnum.QUERY;
connHolder = getConnectionFromDSLocator(hints, isMaster, isSelect);
connHolder.setAutoCommit(true);
connHolder.applyHints(hints);
if (hints.getHA() != null) {
hints.getHA().setDatabaseCategory(connHolder.getMeta().getDatabaseCategory());
}
realDbName = connHolder.getDatabaseName();
} catch (SQLException ex) {
logger.getConnectionFailed(realDbName, ex);
throw ex;
}
return connHolder;
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class DatabaseSelector method getAvailableDbWithFallback.
private String getAvailableDbWithFallback(List<DataBase> primary, List<DataBase> secondary) throws DalException {
if (isNullOrEmpty(primary) && isNullOrEmpty(secondary))
throw new DalException(ErrorCode.NullLogicDbName);
if (designatedDatasource != null) {
if (!DalStatusManager.containsDataSourceStatus(designatedDatasource))
throw new DalException(ErrorCode.InvalidDatabaseKeyName);
if (MarkdownManager.isMarkdown(designatedDatasource))
throw new DalException(ErrorCode.MarkdownConnection, designatedDatasource);
if (ha != null && ha.contains(designatedDatasource)) {
ha.setOver(true);
throw new DalException(ErrorCode.NoMoreConnectionToFailOver);
}
if (containsDesignatedDatasource(primary))
return designatedDatasource;
if (containsDesignatedDatasource(secondary))
return designatedDatasource;
throw new DalException(ErrorCode.InvalidDatabaseKeyName, designatedDatasource);
}
String dbName = getAvailableDb(primary);
if (dbName != null)
return dbName;
dbName = getAvailableDb(secondary);
if (dbName != null)
return dbName;
if (ha != null) {
ha.setOver(true);
throw new DalException(ErrorCode.NoMoreConnectionToFailOver);
}
StringBuilder sb = new StringBuilder(toDbNames(primary));
if (isNullOrEmpty(secondary))
sb.append(", " + toDbNames(secondary));
throw new DalException(ErrorCode.MarkdownConnection, sb.toString());
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class DalConnectionManager method getConnectionFromDSLocator.
private DalConnection getConnectionFromDSLocator(DalHints hints, boolean isMaster, boolean isSelect) throws SQLException {
Connection conn;
String allInOneKey;
DatabaseSet dbSet = config.getDatabaseSet(logicDbName);
String shardId = null;
if (dbSet.isShardingSupported()) {
DalShardingStrategy strategy = dbSet.getStrategy();
// In case the sharding strategy indicate that master shall be used
isMaster |= strategy.isMaster(config, logicDbName, hints);
shardId = hints.getShardId();
if (shardId == null)
shardId = strategy.locateDbShard(config, logicDbName, hints);
if (shardId == null)
throw new DalException(ErrorCode.ShardLocated, logicDbName);
dbSet.validate(shardId);
}
allInOneKey = select(logicDbName, dbSet, hints, shardId, isMaster, isSelect);
try {
conn = locator.getConnection(allInOneKey);
DbMeta meta = DbMeta.createIfAbsent(allInOneKey, dbSet.getDatabaseCategory(), conn);
return new DalConnection(conn, isMaster, shardId, meta);
} catch (Throwable e) {
throw new DalException(ErrorCode.CantGetConnection, e, allInOneKey);
}
}
Aggregations