Skip to content

Commit

Permalink
Merge pull request #157 from njr-11/156-rollback-only-after-rejecting…
Browse files Browse the repository at this point in the history
…-parallel-use

 mark transaction for rollback only after rejection of parallel use
  • Loading branch information
njr-11 authored Apr 16, 2019
2 parents f662222 + 3a6d416 commit f495b91
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion spec/src/main/asciidoc/txcontext.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ try {
stage1.complete(initialCount);
----

A 'ThreadContextProvider' that supports serial use of a propagated transaction, but not parallel use, is allowed to raise `IllegalStateException` upon attempts to associate a JTA transaction to a second thread when the JTA transaction is already active on another thread. The transaction context provider raises `IllegalStateException` upon `ThreadContextSnapshot.begin`, which exceptionally completes the action or task without running it. The `IllegalStateException` should have a meaningful message making it clear to the user that lack of support for the propagation of active transactions for parallel use across multiple threads is the cause of the error.
A 'ThreadContextProvider' that supports serial use of a propagated transaction, but not parallel use, is allowed to raise `IllegalStateException` upon attempts to associate a JTA transaction to a second thread when the JTA transaction is already active on another thread. The transaction context provider raises `IllegalStateException` upon `ThreadContextSnapshot.begin`, which exceptionally completes the action or task without running it. When rejecting parallel use of a transaction, the transaction context provider should also mark the transaction for rollback. The `IllegalStateException` should have a meaningful message making it clear to the user that lack of support for the propagation of active transactions for parallel use across multiple threads is the cause of the error.

The application sees the error when requesting the result of the corresponding stage. For example,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,7 @@ public void propagateTransactionContextJTA() throws Exception {
Method begin = userTransaction.getMethod("begin");
Method commit = userTransaction.getMethod("commit");
Method getStatus = userTransaction.getMethod("getStatus");
Method rollback = userTransaction.getMethod("rollback");

// Propagate context from thread where no transaction is active
CompletableFuture<String> stage0 = executor.newIncompleteFuture();
Expand All @@ -1666,6 +1667,7 @@ public void propagateTransactionContextJTA() throws Exception {

CompletableFuture<String> stage2;

boolean txPropagationRejected = false;
begin.invoke(tx);
try {
// Force stage1 to run on thread where transaction context is already present
Expand All @@ -1691,6 +1693,7 @@ public void propagateTransactionContextJTA() throws Exception {
}
catch (IllegalStateException x) {
System.out.println("Skipping portion of test propagateTransactionContextJTA. Propagation of active transaction is not supported.");
txPropagationRejected = true;
return;
}

Expand All @@ -1702,6 +1705,7 @@ public void propagateTransactionContextJTA() throws Exception {
if (x.getCause() instanceof IllegalStateException) {
System.out.println("Skipping portion of test propagateTransactionContextJTA. " +
"Propagation of active transaction to multiple threads in parallel is not supported.");
txPropagationRejected = true;
return;
}
else {
Expand All @@ -1711,7 +1715,12 @@ public void propagateTransactionContextJTA() throws Exception {
Assert.assertEquals(result, "SUCCESS2");
}
finally {
commit.invoke(tx);
if (txPropagationRejected) {
rollback.invoke(tx);
}
else {
commit.invoke(tx);
}
}
}

Expand Down

0 comments on commit f495b91

Please sign in to comment.