Search in sources :

Example 1 with DalException

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

Example 2 with DalException

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

Example 3 with DalException

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

Example 4 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 5 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)

Aggregations

DalException (com.ctrip.platform.dal.exceptions.DalException)37 ArrayList (java.util.ArrayList)8 SQLException (java.sql.SQLException)5 Test (org.junit.Test)5 DalHints (com.ctrip.platform.dal.dao.DalHints)4 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)4 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)3 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)2 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)2 LogContext (com.ctrip.platform.dal.dao.client.LogContext)2 DatabaseSet (com.ctrip.platform.dal.dao.configure.DatabaseSet)2 DalShardingStrategy (com.ctrip.platform.dal.dao.strategy.DalShardingStrategy)2 Field (java.lang.reflect.Field)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 PooledConnection (org.apache.tomcat.jdbc.pool.PooledConnection)2 DatabaseCategory (com.ctrip.platform.dal.common.enums.DatabaseCategory)1 DalContextClient (com.ctrip.platform.dal.dao.DalContextClient)1 StatementParameter (com.ctrip.platform.dal.dao.StatementParameter)1 Type (com.ctrip.platform.dal.dao.annotation.Type)1