Skip to content

Commit

Permalink
Merge pull request #585 from apache/SCB-1011
Browse files Browse the repository at this point in the history
SCB-1011 Added a NPE check in TransactionAspect
  • Loading branch information
coolbeevip committed Nov 6, 2019
2 parents 4dfdd18 + 00a7f2c commit 8ad3b7b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -55,7 +55,10 @@ Object advise(ProceedingJoinPoint joinPoint, Compensable compensable) throws Thr
if (transactionContext != null) {
populateOmegaContext(context, transactionContext);
}

// SCB-1011 Need to check if the globalTxId transaction is null to avoid the message sending failure
if (context.globalTxId() == null) {
throw new OmegaException("Cannot find the globalTxId from OmegaContext. Please using @SagaStart to start a global transaction.");
}
String localTxId = context.localTxId();
context.newLocalTxId();
LOG.debug("Updated context {} for compensable method {} ", context, method.toString());
Expand Down
Expand Up @@ -22,6 +22,7 @@
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -142,6 +143,17 @@ public void setNewLocalTxIdCompensableWithTransactionContext() throws Throwable
assertThat(omegaContext.localTxId(), is(transactionLocalTxId));
}

@Test
public void globalTxIsNotSet() throws Throwable {
omegaContext.setGlobalTxId(null);
try {
aspect.advise(joinPoint, compensable);
fail("Expect exception here");
} catch (OmegaException ex) {
assertThat(ex.getMessage(), is("Cannot find the globalTxId from OmegaContext. Please using @SagaStart to start a global transaction."));
}
}

@Test
public void newLocalTxIdInCompensable() throws Throwable {
aspect.advise(joinPoint, compensable);
Expand Down

0 comments on commit 8ad3b7b

Please sign in to comment.