Search in sources :

Example 26 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, final ShardExecutionCallback<T> callback) throws SQLException {
    T result = null;
    Throwable error = null;
    LogContext logContext = logger.start(request);
    long startTime = System.currentTimeMillis();
    try {
        request.validateAndPrepare();
        if (request.isCrossShard())
            result = crossShardExecute(logContext, hints, request, callback);
        else
            result = nonCrossShardExecute(logContext, hints, request, callback);
        if (result == null && !nullable)
            throw new DalException(ErrorCode.AssertNull);
        request.endExecution();
    } catch (Throwable e) {
        error = e;
    }
    logContext.setDaoExecuteTime(System.currentTimeMillis() - startTime);
    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 27 with DalException

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

the class SingleUpdateTask method execute.

@Override
public int execute(DalHints hints, Map<String, ?> fields, T rawPojo, DalTaskContext taskContext) throws SQLException {
    if (fields.size() == 0)
        throw new DalException(ErrorCode.ValidateFieldCount);
    Map<String, ?> pks = getPrimaryKeys(fields);
    Object version = getVersion(fields);
    Map<String, ?> filted = null;
    if (rawPojo instanceof UpdatableEntity)
        filted = filterUpdatableEntity(hints, fields, getUpdatedColumns(rawPojo));
    else
        filted = filterNullColumns(hints, fields);
    // If there is no columns changed, we will not perform update
    if (filted.size() == 0)
        return 0;
    String tableName = getRawTableName(hints, fields);
    if (taskContext instanceof DalContextConfigure)
        ((DalContextConfigure) taskContext).addTables(tableName);
    String updateSql = buildUpdateSql(quote(tableName), filted, hints);
    StatementParameters parameters = new StatementParameters();
    addParameters(parameters, filted);
    addParameters(parameters, pks);
    addVersion(parameters, version);
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(updateSql, parameters, hints.setFields(fields), taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) DalException(com.ctrip.platform.dal.exceptions.DalException) UpdatableEntity(com.ctrip.platform.dal.dao.UpdatableEntity) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Example 28 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();
            // Connection discarding moved into datasource level
            /*                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 : PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) SQLException(java.sql.SQLException) DalException(com.ctrip.platform.dal.exceptions.DalException) SQLException(java.sql.SQLException) DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 29 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, final DalTaskContext dalTaskContext) 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>();
            resultParameters.addAll(parameters.getResultParameters());
            for (StatementParameter parameter : parameters.values()) {
                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();
            endExecute();
            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, dalTaskContext);
    return doInConnection(action, hints);
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException)

Example 30 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)) {
        rollback();
        throw new DalException(ErrorCode.TransactionLevelMatch, (level - 1), startLevel);
    }
    if (level > FIRST_LEVEL) {
        setTransactionStatusOnCommit();
        level--;
        return;
    }
    if (status == DalTransactionStatus.Rollback || status == DalTransactionStatus.Conflict) {
        setTransactionStatusOnCommit();
        rollbackIfNeeded();
        throwExceptionOnCommitConflicted();
    }
    try {
        processRollbackOnlyIfExist();
    } catch (Throwable e) {
        throw new DalException(e.getMessage(), e);
    }
    commitIfNeeded();
}
Also used : DalException(com.ctrip.platform.dal.exceptions.DalException)

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