### Question:
SagaStartAspect { @Around("execution(@org.apache.servicecomb.saga.omega.context.annotations.SagaStart * *(..)) && @annotation(sagaStart)") Object advise(ProceedingJoinPoint joinPoint, SagaStart sagaStart) throws Throwable { long a = System.nanoTime(); Method method = null; boolean isProceed = false; try { initializeOmegaContext(sagaStart); method = ((MethodSignature) joinPoint.getSignature()).getMethod(); AlphaResponse alphaResponse = sagaStartAnnotationProcessor.preIntercept(context.globalTxId(), method.toString(), sagaStart.timeout(), "", 0); if (!alphaResponse.enabledTx()) { CompensableSqlMetrics.setIsMonitorSql(ApplicationContextUtil.getApplicationContext().getBean(MessageSender.class).readConfigFromServer(ConfigCenterType.SqlMonitor.toInteger(), context.category()).getStatus()); } isProceed = true; Object result = joinPoint.proceed(); if (alphaResponse.enabledTx()) { sagaStartAnnotationProcessor.postIntercept(context.globalTxId(), method.toString()); } return result; } catch (Throwable throwable) { boolean isFaultTolerant = ApplicationContextUtil.getApplicationContext().getBean(MessageSender.class).readConfigFromServer(ConfigCenterType.GlobalTxFaultTolerant.toInteger(), context.category()).getStatus(); if (!(throwable instanceof OmegaException) && !isFaultTolerant) { try { sagaStartAnnotationProcessor.onError(context.globalTxId(), method == null ? null : method.toString(), throwable); LOG.error("Transaction {} failed.", context.globalTxId()); } catch (Throwable e) { throw e; } } if (!isProceed && isFaultTolerant) { return joinPoint.proceed(); } throw throwable; } finally { context.clear(); } } SagaStartAspect(MessageSender sender, OmegaContext context);   }### Answer:
@Test public void newGlobalTxIdInSagaStart() throws Throwable { aspect.advise(joinPoint, sagaStart); TxEvent startedEvent = messages.get(0); assertThat(startedEvent.globalTxId(), is(globalTxId)); assertThat(startedEvent.localTxId(), is(globalTxId)); assertThat(startedEvent.parentTxId(), is(nullValue())); assertThat(startedEvent.type(), is(EventType.SagaStartedEvent)); TxEvent endedEvent = messages.get(1); assertThat(endedEvent.globalTxId(), is(globalTxId)); assertThat(endedEvent.localTxId(), is(globalTxId)); assertThat(endedEvent.parentTxId(), is(nullValue())); assertThat(endedEvent.type(), is(EventType.SagaEndedEvent)); assertThat(omegaContext.globalTxId(), is(nullValue())); assertThat(omegaContext.localTxId(), is(nullValue())); }
@Test public void clearContextOnSagaStartError() throws Throwable { RuntimeException oops = new RuntimeException("oops"); when(joinPoint.proceed()).thenThrow(oops); try { aspect.advise(joinPoint, sagaStart); expectFailing(RuntimeException.class); } catch (RuntimeException e) { assertThat(e, is(oops)); } assertThat(messages.size(), is(2)); TxEvent event = messages.get(0); assertThat(event.globalTxId(), is(globalTxId)); assertThat(event.localTxId(), is(globalTxId)); assertThat(event.parentTxId(), is(nullValue())); assertThat(event.type(), is(EventType.SagaStartedEvent)); event = messages.get(1); assertThat(event.globalTxId(), is(globalTxId)); assertThat(event.localTxId(), is(globalTxId)); assertThat(event.parentTxId(), is(nullValue())); assertThat(event.type(), is(EventType.TxAbortedEvent)); assertThat(omegaContext.globalTxId(), is(nullValue())); assertThat(omegaContext.localTxId(), is(nullValue())); }