Search in sources :

Example 1 with TransactionException

use of io.seata.core.exception.TransactionException in project seata by seata.

the class AbstractTCInboundHandler method handle.

@Override
public BranchReportResponse handle(BranchReportRequest request, final RpcContext rpcContext) {
    BranchReportResponse response = new BranchReportResponse();
    exceptionHandleTemplate(new AbstractCallback<BranchReportRequest, BranchReportResponse>() {

        @Override
        public void execute(BranchReportRequest request, BranchReportResponse response) throws TransactionException {
            try {
                doBranchReport(request, response, rpcContext);
            } catch (StoreException e) {
                throw new TransactionException(TransactionExceptionCode.FailedStore, String.format("branch report request failed. xid=%s, branchId=%s, msg=%s", request.getXid(), request.getBranchId(), e.getMessage()), e);
            }
        }
    }, request, response);
    return response;
}
Also used : TransactionException(io.seata.core.exception.TransactionException) BranchReportResponse(io.seata.core.protocol.transaction.BranchReportResponse) BranchReportRequest(io.seata.core.protocol.transaction.BranchReportRequest) StoreException(io.seata.common.exception.StoreException)

Example 2 with TransactionException

use of io.seata.core.exception.TransactionException in project seata by seata.

the class AbstractTCInboundHandler method handle.

@Override
public GlobalLockQueryResponse handle(GlobalLockQueryRequest request, final RpcContext rpcContext) {
    GlobalLockQueryResponse response = new GlobalLockQueryResponse();
    exceptionHandleTemplate(new AbstractCallback<GlobalLockQueryRequest, GlobalLockQueryResponse>() {

        @Override
        public void execute(GlobalLockQueryRequest request, GlobalLockQueryResponse response) throws TransactionException {
            try {
                doLockCheck(request, response, rpcContext);
            } catch (StoreException e) {
                throw new TransactionException(TransactionExceptionCode.FailedStore, String.format("global lock query request failed. xid=%s, msg=%s", request.getXid(), e.getMessage()), e);
            }
        }
    }, request, response);
    return response;
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalLockQueryRequest(io.seata.core.protocol.transaction.GlobalLockQueryRequest) GlobalLockQueryResponse(io.seata.core.protocol.transaction.GlobalLockQueryResponse) StoreException(io.seata.common.exception.StoreException)

Example 3 with TransactionException

use of io.seata.core.exception.TransactionException in project seata by seata.

the class AbstractTCInboundHandler method handle.

@Override
public GlobalRollbackResponse handle(GlobalRollbackRequest request, final RpcContext rpcContext) {
    GlobalRollbackResponse response = new GlobalRollbackResponse();
    response.setGlobalStatus(GlobalStatus.Rollbacking);
    exceptionHandleTemplate(new AbstractCallback<GlobalRollbackRequest, GlobalRollbackResponse>() {

        @Override
        public void execute(GlobalRollbackRequest request, GlobalRollbackResponse response) throws TransactionException {
            try {
                doGlobalRollback(request, response, rpcContext);
            } catch (StoreException e) {
                throw new TransactionException(TransactionExceptionCode.FailedStore, String.format("global rollback request failed. xid=%s, msg=%s", request.getXid(), e.getMessage()), e);
            }
        }

        @Override
        public void onTransactionException(GlobalRollbackRequest request, GlobalRollbackResponse response, TransactionException tex) {
            super.onTransactionException(request, response, tex);
            // may be appears StoreException outer layer method catch
            checkTransactionStatus(request, response);
        }

        @Override
        public void onException(GlobalRollbackRequest request, GlobalRollbackResponse response, Exception rex) {
            super.onException(request, response, rex);
            // may be appears StoreException outer layer method catch
            checkTransactionStatus(request, response);
        }
    }, request, response);
    return response;
}
Also used : GlobalRollbackRequest(io.seata.core.protocol.transaction.GlobalRollbackRequest) TransactionException(io.seata.core.exception.TransactionException) GlobalRollbackResponse(io.seata.core.protocol.transaction.GlobalRollbackResponse) TransactionException(io.seata.core.exception.TransactionException) StoreException(io.seata.common.exception.StoreException) StoreException(io.seata.common.exception.StoreException)

Example 4 with TransactionException

use of io.seata.core.exception.TransactionException in project seata by seata.

the class DefaultCore method doGlobalRollback.

@Override
public boolean doGlobalRollback(GlobalSession globalSession, boolean retrying) throws TransactionException {
    boolean success = true;
    // start rollback event
    eventBus.post(new GlobalTransactionEvent(globalSession.getTransactionId(), GlobalTransactionEvent.ROLE_TC, globalSession.getTransactionName(), globalSession.getApplicationId(), globalSession.getTransactionServiceGroup(), globalSession.getBeginTime(), null, globalSession.getStatus()));
    if (globalSession.isSaga()) {
        success = getCore(BranchType.SAGA).doGlobalRollback(globalSession, retrying);
    } else {
        Boolean result = SessionHelper.forEach(globalSession.getReverseSortedBranches(), branchSession -> {
            BranchStatus currentBranchStatus = branchSession.getStatus();
            if (currentBranchStatus == BranchStatus.PhaseOne_Failed) {
                globalSession.removeBranch(branchSession);
                return CONTINUE;
            }
            try {
                BranchStatus branchStatus = branchRollback(globalSession, branchSession);
                switch(branchStatus) {
                    case PhaseTwo_Rollbacked:
                        globalSession.removeBranch(branchSession);
                        LOGGER.info("Rollback branch transaction successfully, xid = {} branchId = {}", globalSession.getXid(), branchSession.getBranchId());
                        return CONTINUE;
                    case PhaseTwo_RollbackFailed_Unretryable:
                        SessionHelper.endRollbackFailed(globalSession);
                        LOGGER.info("Rollback branch transaction fail and stop retry, xid = {} branchId = {}", globalSession.getXid(), branchSession.getBranchId());
                        return false;
                    default:
                        LOGGER.info("Rollback branch transaction fail and will retry, xid = {} branchId = {}", globalSession.getXid(), branchSession.getBranchId());
                        if (!retrying) {
                            globalSession.queueToRetryRollback();
                        }
                        return false;
                }
            } catch (Exception ex) {
                StackTraceLogger.error(LOGGER, ex, "Rollback branch transaction exception, xid = {} branchId = {} exception = {}", new String[] { globalSession.getXid(), String.valueOf(branchSession.getBranchId()), ex.getMessage() });
                if (!retrying) {
                    globalSession.queueToRetryRollback();
                }
                throw new TransactionException(ex);
            }
        });
        // Return if the result is not null
        if (result != null) {
            return result;
        }
        // In db mode, there is a problem of inconsistent data in multiple copies, resulting in new branch
        // transaction registration when rolling back.
        // 1. New branch transaction and rollback branch transaction have no data association
        // 2. New branch transaction has data association with rollback branch transaction
        // The second query can solve the first problem, and if it is the second problem, it may cause a rollback
        // failure due to data changes.
        GlobalSession globalSessionTwice = SessionHolder.findGlobalSession(globalSession.getXid());
        if (globalSessionTwice != null && globalSessionTwice.hasBranch()) {
            LOGGER.info("Rollbacking global transaction is NOT done, xid = {}.", globalSession.getXid());
            return false;
        }
    }
    if (success) {
        SessionHelper.endRollbacked(globalSession);
        // rollbacked event
        eventBus.post(new GlobalTransactionEvent(globalSession.getTransactionId(), GlobalTransactionEvent.ROLE_TC, globalSession.getTransactionName(), globalSession.getApplicationId(), globalSession.getTransactionServiceGroup(), globalSession.getBeginTime(), System.currentTimeMillis(), globalSession.getStatus()));
        LOGGER.info("Rollback global transaction successfully, xid = {}.", globalSession.getXid());
    }
    return success;
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalSession(io.seata.server.session.GlobalSession) BranchStatus(io.seata.core.model.BranchStatus) GlobalTransactionEvent(io.seata.core.event.GlobalTransactionEvent) TransactionException(io.seata.core.exception.TransactionException) NotSupportYetException(io.seata.common.exception.NotSupportYetException)

Example 5 with TransactionException

use of io.seata.core.exception.TransactionException in project seata by seata.

the class DefaultSagaTransactionalTemplate method reportTransaction.

@Override
public void reportTransaction(GlobalTransaction tx, GlobalStatus globalStatus) throws TransactionalExecutor.ExecutionException {
    try {
        tx.globalReport(globalStatus);
        triggerAfterCompletion();
    } catch (TransactionException txe) {
        throw new TransactionalExecutor.ExecutionException(tx, txe, TransactionalExecutor.Code.ReportFailure);
    }
}
Also used : TransactionException(io.seata.core.exception.TransactionException) ExecutionException(io.seata.tm.api.TransactionalExecutor.ExecutionException) TransactionalExecutor(io.seata.tm.api.TransactionalExecutor)

Aggregations

TransactionException (io.seata.core.exception.TransactionException)54 BranchStatus (io.seata.core.model.BranchStatus)14 GlobalSession (io.seata.server.session.GlobalSession)9 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)7 StoreException (io.seata.common.exception.StoreException)7 ExecutionException (io.seata.tm.api.TransactionalExecutor.ExecutionException)6 GlobalTransaction (io.seata.tm.api.GlobalTransaction)5 IOException (java.io.IOException)5 TimeoutException (java.util.concurrent.TimeoutException)5 NotSupportYetException (io.seata.common.exception.NotSupportYetException)4 GlobalTransactionEvent (io.seata.core.event.GlobalTransactionEvent)4 GlobalTransactionException (io.seata.core.exception.GlobalTransactionException)4 GlobalStatus (io.seata.core.model.GlobalStatus)4 EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)4 BranchSession (io.seata.server.session.BranchSession)4 SQLException (java.sql.SQLException)4 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)3 TransactionalExecutor (io.seata.tm.api.TransactionalExecutor)3 XAException (javax.transaction.xa.XAException)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3