Search in sources :

Example 1 with GlobalTransaction

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

the class DbAndReportTcStateLogStore method beginTransaction.

protected void beginTransaction(StateMachineInstance machineInstance, ProcessContext context) {
    if (sagaTransactionalTemplate != null) {
        StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
        TransactionInfo transactionInfo = new TransactionInfo();
        transactionInfo.setTimeOut(stateMachineConfig.getTransOperationTimeout());
        transactionInfo.setName(Constants.SAGA_TRANS_NAME_PREFIX + machineInstance.getStateMachine().getName());
        try {
            GlobalTransaction globalTransaction = sagaTransactionalTemplate.beginTransaction(transactionInfo);
            machineInstance.setId(globalTransaction.getXid());
            context.setVariable(DomainConstants.VAR_NAME_GLOBAL_TX, globalTransaction);
            Map<String, Object> machineContext = machineInstance.getContext();
            if (machineContext != null) {
                machineContext.put(DomainConstants.VAR_NAME_GLOBAL_TX, globalTransaction);
            }
        } catch (ExecutionException e) {
            String xid = null;
            if (e.getTransaction() != null) {
                xid = e.getTransaction().getXid();
            }
            throw new EngineExecutionException(e, e.getCode() + ", TransName:" + transactionInfo.getName() + ", XID: " + xid + ", Reason: " + e.getMessage(), FrameworkErrorCode.TransactionManagerError);
        } finally {
            if (Boolean.TRUE.equals(context.getVariable(DomainConstants.VAR_NAME_IS_ASYNC_EXECUTION))) {
                RootContext.unbind();
                RootContext.unbindBranchType();
            }
        }
    }
}
Also used : TransactionInfo(io.seata.tm.api.transaction.TransactionInfo) GlobalTransaction(io.seata.tm.api.GlobalTransaction) DbStateMachineConfig(io.seata.saga.engine.config.DbStateMachineConfig) DefaultStateMachineConfig(io.seata.saga.engine.impl.DefaultStateMachineConfig) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) ExecutionException(io.seata.tm.api.TransactionalExecutor.ExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException)

Example 2 with GlobalTransaction

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

the class StateMachineDBTests method testStatusMatchingStateMachine.

@Test
public void testStatusMatchingStateMachine() throws Exception {
    long start = System.currentTimeMillis();
    Map<String, Object> paramMap = new HashMap<>(1);
    paramMap.put("a", 1);
    paramMap.put("barThrowException", "true");
    String stateMachineName = "simpleStatusMatchingStateMachine";
    StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap);
    long cost = System.currentTimeMillis() - start;
    System.out.println("====== cost :" + cost);
    Assertions.assertNotNull(inst.getException());
    Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus()));
    GlobalTransaction globalTransaction = getGlobalTransaction(inst);
    Assertions.assertNotNull(globalTransaction);
    System.out.println(globalTransaction.getStatus());
    Assertions.assertTrue(GlobalStatus.CommitRetrying.equals(globalTransaction.getStatus()));
}
Also used : HashMap(java.util.HashMap) GlobalTransaction(io.seata.tm.api.GlobalTransaction) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) Test(org.junit.jupiter.api.Test)

Example 3 with GlobalTransaction

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

the class StateMachineDBTests method testCompensationStateMachineForRecovery.

@Test
public void testCompensationStateMachineForRecovery() throws Exception {
    long start = System.currentTimeMillis();
    Map<String, Object> paramMap = new HashMap<>(1);
    paramMap.put("a", 1);
    paramMap.put("fooThrowExceptionRandomly", "true");
    paramMap.put("barThrowExceptionRandomly", "true");
    paramMap.put("compensateFooThrowExceptionRandomly", "true");
    paramMap.put("compensateBarThrowExceptionRandomly", "true");
    String stateMachineName = "simpleCompensationStateMachineForRecovery";
    StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap);
    long cost = System.currentTimeMillis() - start;
    System.out.println("====== cost :" + cost);
    GlobalTransaction globalTransaction = getGlobalTransaction(inst);
    Assertions.assertNotNull(globalTransaction);
    System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
    // waiting for global transaction recover
    while (!(ExecutionStatus.SU.equals(inst.getStatus()) || ExecutionStatus.SU.equals(inst.getCompensationStatus()))) {
        System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
        Thread.sleep(2000);
        inst = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance(inst.getId());
    }
}
Also used : HashMap(java.util.HashMap) GlobalTransaction(io.seata.tm.api.GlobalTransaction) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) Test(org.junit.jupiter.api.Test)

Example 4 with GlobalTransaction

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

the class StateMachineDBTests method doTestStateMachineTransTimeoutAsync.

private void doTestStateMachineTransTimeoutAsync(Map<String, Object> paramMap) throws Exception {
    long start = System.currentTimeMillis();
    String stateMachineName = "simpleCompensationStateMachine";
    StateMachineInstance inst = stateMachineEngine.startAsync(stateMachineName, null, paramMap, callback);
    waittingForFinish(inst);
    long cost = System.currentTimeMillis() - start;
    System.out.println("====== cost :" + cost);
    GlobalTransaction globalTransaction = getGlobalTransaction(inst);
    Assertions.assertNotNull(globalTransaction);
    System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
    // waiting for global transaction recover
    while (!ExecutionStatus.SU.equals(inst.getCompensationStatus())) {
        System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
        Thread.sleep(2000);
        inst = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance(inst.getId());
    }
    Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus()) || ExecutionStatus.SU.equals(inst.getStatus()));
    Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getCompensationStatus()));
}
Also used : GlobalTransaction(io.seata.tm.api.GlobalTransaction) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 5 with GlobalTransaction

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

the class StateMachineDBTests method doTestStateMachineCustomRecoverStrategyOnTimeoutAsync.

private void doTestStateMachineCustomRecoverStrategyOnTimeoutAsync(Map<String, Object> paramMap) throws Exception {
    long start = System.currentTimeMillis();
    String stateMachineName = "simpleStateMachineWithRecoverStrategy";
    StateMachineInstance inst = stateMachineEngine.startAsync(stateMachineName, null, paramMap, callback);
    waittingForFinish(inst);
    long cost = System.currentTimeMillis() - start;
    System.out.println("====== cost :" + cost);
    GlobalTransaction globalTransaction = getGlobalTransaction(inst);
    Assertions.assertNotNull(globalTransaction);
    System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
    // waiting for global transaction recover
    while (!(ExecutionStatus.SU.equals(inst.getStatus()) && GlobalStatus.Finished.equals(globalTransaction.getStatus()))) {
        System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
        System.out.println("====== StateMachineInstanceStatus: " + inst.getStatus());
        Thread.sleep(2000);
        inst = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance(inst.getId());
    }
    Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getStatus()));
    Assertions.assertNull(inst.getCompensationStatus());
}
Also used : GlobalTransaction(io.seata.tm.api.GlobalTransaction) 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