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();
}
}
}
}
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()));
}
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());
}
}
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()));
}
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());
}
Aggregations