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);
}
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class DalDirectClient method call.
@Override
public Map<String, ?> call(String callString, StatementParameters parameters, final DalHints hints) throws SQLException {
ConnectionAction<Map<String, ?>> action = new ConnectionAction<Map<String, ?>>() {
@Override
public Map<String, ?> execute() throws Exception {
List<StatementParameter> resultParameters = new ArrayList<StatementParameter>();
List<StatementParameter> callParameters = new ArrayList<StatementParameter>();
for (StatementParameter parameter : parameters.values()) {
if (parameter.isResultsParameter()) {
resultParameters.add(parameter);
} else if (parameter.isOutParameter()) {
callParameters.add(parameter);
}
}
if (hints.is(DalHintEnum.retrieveAllSpResults) && resultParameters.size() > 0)
throw new DalException("Dal hint 'autoRetrieveAllResults' should only be used when there is no special result parameter specified");
conn = getConnection(hints, this);
callableStatement = createCallableStatement(conn, callString, parameters, hints);
beginExecute();
boolean retVal = executeCall(callableStatement, entry);
int updateCount = callableStatement.getUpdateCount();
endExectue();
Map<String, Object> returnedResults = new LinkedHashMap<String, Object>();
if (retVal || updateCount != -1) {
returnedResults.putAll(extractReturnedResults(callableStatement, resultParameters, updateCount, hints));
}
returnedResults.putAll(extractOutputParameters(callableStatement, callParameters));
return returnedResults;
}
};
action.populateSp(callString, parameters);
return doInConnection(action, hints);
}
use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.
the class DefaultDatabaseSelector method select.
@Override
public String select(SelectionContext context) throws DalException {
String designatedDatasource = context.getDesignatedDatabase();
DalHA ha = context.getHa();
List<DataBase> primary;
List<DataBase> secondary = null;
if (context.getHints().is(DalHintEnum.slaveOnly)) {
primary = context.getSlaves();
} else if (context.isMasterOnly() || !context.isSelect()) {
primary = context.getMasters();
} else {
primary = context.getSlaves();
secondary = context.getMasters();
}
if (isNullOrEmpty(primary) && isNullOrEmpty(secondary))
throw new DalException(ErrorCode.NullLogicDbName);
if (designatedDatasource != null) {
if (!DalStatusManager.containsDataSourceStatus(designatedDatasource))
throw new DalException(ErrorCode.InvalidDatabaseKeyName, designatedDatasource);
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(designatedDatasource, primary))
return designatedDatasource;
if (containsDesignatedDatasource(designatedDatasource, secondary))
return designatedDatasource;
throw new DalException(ErrorCode.InvalidDatabaseKeyName, designatedDatasource);
}
String dbName = getAvailableDb(ha, primary);
if (dbName != null)
return dbName;
dbName = getAvailableDb(ha, 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 PartialQueryTableDaoUnitTest method testDetectFieldNotExist.
@Test
public void testDetectFieldNotExist() throws Exception {
DalTableDao<PersonWithoutName> client = new DalTableDao<>(new DalDefaultJpaParser<>(PersonWithoutName.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", "PeopleID", "CountryID");
builder.in("PeopleID", peopleIds, Types.INTEGER, false);
builder.and();
builder.in("CityID", cityIds, Types.INTEGER, false);
try {
client.query(builder, new DalHints().inAllShards().inTableShard(1));
Assert.fail();
} catch (DalException e) {
e.printStackTrace();
assertEquals(ErrorCode.FieldNotExists.getCode(), e.getErrorCode());
}
}
Aggregations