Search in sources :

Example 26 with GlobalTransaction

use of io.seata.tm.api.GlobalTransaction in project seata by seata.

the class DbAndReportTcStateLogStore method reportTransactionFinished.

protected void reportTransactionFinished(StateMachineInstance machineInstance, ProcessContext context) {
    if (sagaTransactionalTemplate != null) {
        try {
            GlobalTransaction globalTransaction = getGlobalTransaction(machineInstance, context);
            if (globalTransaction == null) {
                throw new EngineExecutionException("Global transaction is not exists", FrameworkErrorCode.ObjectNotExists);
            }
            GlobalStatus globalStatus;
            if (ExecutionStatus.SU.equals(machineInstance.getStatus()) && machineInstance.getCompensationStatus() == null) {
                globalStatus = GlobalStatus.Committed;
            } else if (ExecutionStatus.SU.equals(machineInstance.getCompensationStatus())) {
                globalStatus = GlobalStatus.Rollbacked;
            } else if (ExecutionStatus.FA.equals(machineInstance.getCompensationStatus()) || ExecutionStatus.UN.equals(machineInstance.getCompensationStatus())) {
                globalStatus = GlobalStatus.RollbackRetrying;
            } else if (ExecutionStatus.FA.equals(machineInstance.getStatus()) && machineInstance.getCompensationStatus() == null) {
                globalStatus = GlobalStatus.Finished;
            } else if (ExecutionStatus.UN.equals(machineInstance.getStatus()) && machineInstance.getCompensationStatus() == null) {
                globalStatus = GlobalStatus.CommitRetrying;
            } else {
                globalStatus = GlobalStatus.UnKnown;
            }
            sagaTransactionalTemplate.reportTransaction(globalTransaction, globalStatus);
        } catch (ExecutionException e) {
            LOGGER.error("Report transaction finish to server error: {}, StateMachine: {}, XID: {}, Reason: {}", e.getCode(), machineInstance.getStateMachine().getName(), machineInstance.getId(), e.getMessage(), e);
        } catch (TransactionException e) {
            LOGGER.error("Report transaction finish to server error: {}, StateMachine: {}, XID: {}, Reason: {}", e.getCode(), machineInstance.getStateMachine().getName(), machineInstance.getId(), e.getMessage(), e);
        } finally {
            // clear
            RootContext.unbind();
            RootContext.unbindBranchType();
            sagaTransactionalTemplate.triggerAfterCompletion();
            sagaTransactionalTemplate.cleanUp();
        }
    }
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalStatus(io.seata.core.model.GlobalStatus) GlobalTransaction(io.seata.tm.api.GlobalTransaction) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) ExecutionException(io.seata.tm.api.TransactionalExecutor.ExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException)

Example 27 with GlobalTransaction

use of io.seata.tm.api.GlobalTransaction in project seata by seata.

the class InSagaBranchHandlerInterceptor method getXidFromProcessContext.

/**
 * Gets xid from saga process context.
 *
 * @return the xid
 */
protected String getXidFromProcessContext(ProcessContext context) {
    String xid = null;
    Map<String, Object> contextVariable = (Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT);
    if (contextVariable != null && contextVariable.containsKey(DomainConstants.VAR_NAME_GLOBAL_TX)) {
        GlobalTransaction globalTransaction = (GlobalTransaction) contextVariable.get(DomainConstants.VAR_NAME_GLOBAL_TX);
        xid = globalTransaction.getXid();
    } else {
        StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
        if (stateMachineInstance != null) {
            xid = stateMachineInstance.getId();
        }
    }
    return xid;
}
Also used : GlobalTransaction(io.seata.tm.api.GlobalTransaction) Map(java.util.Map) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Aggregations

GlobalTransaction (io.seata.tm.api.GlobalTransaction)27 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)17 Test (org.junit.jupiter.api.Test)13 HashMap (java.util.HashMap)10 Disabled (org.junit.jupiter.api.Disabled)7 EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)6 TransactionException (io.seata.core.exception.TransactionException)5 ExecutionException (io.seata.tm.api.TransactionalExecutor.ExecutionException)5 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)4 DruidXADataSource (com.alibaba.druid.pool.xa.DruidXADataSource)4 MysqlXADataSource (com.mysql.jdbc.jdbc2.optional.MysqlXADataSource)4 DataSource (javax.sql.DataSource)4 XADataSource (javax.sql.XADataSource)4 PGXADataSource (org.postgresql.xa.PGXADataSource)4 StateMachineConfig (io.seata.saga.engine.StateMachineConfig)3 DbStateMachineConfig (io.seata.saga.engine.config.DbStateMachineConfig)3 DefaultStateMachineConfig (io.seata.saga.engine.impl.DefaultStateMachineConfig)3 BranchStatus (io.seata.core.model.BranchStatus)1 GlobalStatus (io.seata.core.model.GlobalStatus)1 StateInstance (io.seata.saga.statelang.domain.StateInstance)1