Search in sources :

Example 11 with DalException

use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.

the class DalRequestExecutor method internalExecute.

private <T> T internalExecute(DalHints hints, DalRequest<T> request, boolean nullable) throws SQLException {
    T result = null;
    Throwable error = null;
    LogContext logContext = logger.start(request);
    try {
        request.validate();
        if (request.isCrossShard())
            result = crossShardExecute(logContext, hints, request);
        else
            result = nonCrossShardExecute(logContext, hints, request);
        if (result == null && !nullable)
            throw new DalException(ErrorCode.AssertNull);
        request.endExecution();
    } catch (Throwable e) {
        error = e;
    }
    logger.end(logContext, error);
    handleCallback(hints, result, error);
    if (error != null)
        throw DalException.wrap(error);
    return result;
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException) LogContext(com.ctrip.platform.dal.dao.client.LogContext)

Example 12 with DalException

use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.

the class ConnectionActionTest method testCleanupCloseConnection.

@Test
public void testCleanupCloseConnection() {
    SQLException e1 = new SQLException("test discard", "1234");
    e1.setNextException(new SQLException("test discard", "08006"));
    SQLException e2 = new SQLException("test discard", "1234");
    e2.setNextException(new SQLException("test discard", "08S01"));
    Exception[] el = new Exception[] { // Case 1 detect direct SQLException
    new SQLException("test discard", "08006"), // Case 2 detect embedded SQLException wrapped by DalException
    new DalException("test discard", new SQLException("test discard", "08006")), // Case 3 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", new SQLException("test discard", "08006")), // Case 4 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", e1), // Case 1 detect direct SQLException
    new SQLException("test discard", "08006"), // Case 2 detect embedded SQLException wrapped by DalException
    new DalException("test discard", new SQLException("test discard", "08006")), // Case 3 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", new SQLException("test discard", "08006")), // Case 4 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", e2) };
    for (Exception e : el) {
        try {
            TestConnectionAction test = new TestConnectionAction();
            DalConnection connHolder = getDalConnection();
            test.connHolder = connHolder;
            test.statement = test.connHolder.getConn().createStatement();
            test.rs = test.statement.executeQuery("select * from " + SqlServerTestInitializer.TABLE_NAME);
            test.rs.next();
            PooledConnection c = (PooledConnection) connHolder.getConn().unwrap(PooledConnection.class);
            connHolder.error(e);
            test.cleanup();
            assertTrue(c.isDiscarded());
            assertTrue(c.isReleased());
            assertNotNull(test);
            assertTrue(test.conn == null);
            assertTrue(test.statement == null);
            assertTrue(test.rs == null);
            assertTrue(test.connHolder == null);
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("There should be no exception here");
        }
    }
}
Also used : DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) SQLException(java.sql.SQLException) DalException(com.ctrip.platform.dal.exceptions.DalException) DalException(com.ctrip.platform.dal.exceptions.DalException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 13 with DalException

use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.

the class DalSingleResultExtractor method extract.

@Override
public T extract(ResultSet rs) throws SQLException {
    T result = null;
    checkHints(rs);
    if (rs.next()) {
        result = mapper.map(rs, 0);
        if (rs.next() && requireSingle)
            throw new DalException(ErrorCode.AssertSingle);
    }
    return result;
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 14 with DalException

use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.

the class DalTransaction method endTransaction.

public void endTransaction(int startLevel) throws SQLException {
    if (rolledBack || completed)
        throw new DalException(ErrorCode.TransactionState);
    if (startLevel != (level - 1)) {
        rollbackTransaction();
        throw new DalException(ErrorCode.TransactionLevelMatch, (level - 1), startLevel);
    }
    if (level > 1) {
        level--;
        return;
    }
    // Back to the first transaction, about to commit
    beforeCommit();
    level = 0;
    completed = true;
    cleanup(true);
    afterCommit();
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 15 with DalException

use of com.ctrip.platform.dal.exceptions.DalException in project dal by ctripcorp.

the class PartialQueryQueryDaoTest method testIgnorMissingFields.

@Test
public void testIgnorMissingFields() 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();
    FreeEntityPartialPojo ret;
    try {
        hints = new DalHints();
        ret = findFreeFirstBigger(name, cityIds, hints.inShard(1).partialQuery("PeopleID", "Name", "CityID", "ProvinceID"));
        fail();
    } catch (DalException e) {
        Assert.assertEquals(ErrorCode.FieldNotExists.getCode(), e.getErrorCode());
    }
    hints = new DalHints().ignoreMissingFields().partialQuery("PeopleID", "Name", "CityID", "ProvinceID");
    ret = findFreeFirstBigger(name, cityIds, hints.inShard(1));
    assertNotNull(ret);
    hints = new DalHints().ignoreMissingFields().partialQuery("PeopleID", "Name", "CityID", "ProvinceID");
    ret = findFreeFirstBigger(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)

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