Skip to content

Commit

Permalink
defer throwing an exception from beforeCompletion synchs
Browse files Browse the repository at this point in the history
  • Loading branch information
mmusgrov committed Nov 13, 2023
1 parent 5f5c644 commit d80b30e
Showing 1 changed file with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public void add(Synchronization synchronization) {

@Override
public void beforeCompletion() {
Throwable deferredThrowable = null;

status = ExecutionStatus.RUNNING;

// Note that because synchronizations can register other synchronizations
Expand All @@ -75,29 +77,25 @@ public void beforeCompletion() {
"The synchronization %s associated with tx key %s failed during beforeCompletion: %s",
sync, tsr.getTransactionKey(), e.getMessage());
}
throw e;

if (deferredThrowable == null) {
deferredThrowable = e;
}
}
}

status = ExecutionStatus.FINISHED;

if (deferredThrowable != null) {
throw new RuntimeException(deferredThrowable);
}
}

@Override
public void afterCompletion(int status) {
// The list should be iterated in reverse order
for (int i = synchs.size(); i-- > 0;) {
Synchronization sync = synchs.get(i);

try {
sync.afterCompletion(status);
} catch (Exception e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debugf(
"The synchronization %s associated with tx key %s failed during afterCompletion(%d): %s",
sync, tsr.getTransactionKey(), status, e.getMessage());
}
throw e;
}
synchs.get(i).afterCompletion(status);
}
}

Expand Down

0 comments on commit d80b30e

Please sign in to comment.