Search in sources :

Example 6 with DalException

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());
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 7 with DalException

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);
    }
}
Also used : DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) DalException(com.ctrip.platform.dal.exceptions.DalException) Connection(java.sql.Connection)

Example 8 with DalException

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);
}
Also used : ArrayList(java.util.ArrayList) StatementParameter(com.ctrip.platform.dal.dao.StatementParameter) LinkedHashMap(java.util.LinkedHashMap) DalException(com.ctrip.platform.dal.exceptions.DalException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with DalException

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());
}
Also used : DalHA(com.ctrip.platform.dal.dao.client.DalHA) DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 10 with DalException

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());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Aggregations

DalException (com.ctrip.platform.dal.exceptions.DalException)21 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)5 DalHints (com.ctrip.platform.dal.dao.DalHints)4 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)3 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)2 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)2 SQLException (java.sql.SQLException)2 Map (java.util.Map)2 StatementParameter (com.ctrip.platform.dal.dao.StatementParameter)1 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)1 DalConnection (com.ctrip.platform.dal.dao.client.DalConnection)1 DalHA (com.ctrip.platform.dal.dao.client.DalHA)1 LogContext (com.ctrip.platform.dal.dao.client.LogContext)1 DatabaseSet (com.ctrip.platform.dal.dao.configure.DatabaseSet)1 DalShardingStrategy (com.ctrip.platform.dal.dao.strategy.DalShardingStrategy)1 Field (java.lang.reflect.Field)1 Connection (java.sql.Connection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1