This is an automated email from the ASF dual-hosted git repository. danhaywood pushed a commit to branch CAUSEWAY-3738 in repository https://gitbox.apache.org/repos/asf/causeway.git
commit 81bd0f017857072502ff37da443816a1992763a1 Author: danhaywood <[email protected]> AuthorDate: Wed May 8 22:43:46 2024 +0100 CAUSEWAY-3738: factors out executeCommandWithinOwnTransactionElseFaile and also logAndCaptureFailure --- .../applib/job/RunBackgroundCommandsJob.java | 64 ++++++++++++---------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java index ee57bd3545..16c9371d95 100644 --- a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java +++ b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java @@ -116,37 +116,45 @@ public class RunBackgroundCommandsJob implements Job { private void executeWithinOwnTransaction(CommandDto commandDto, InteractionContext interactionContext) { interactionService.runAndCatch(interactionContext, () -> { - transactionService.runTransactional(Propagation.REQUIRES_NEW, () -> { - // look up the CommandLogEntry again because we are within a new transaction. - val commandLogEntryIfAny = commandLogEntryRepository.findByInteractionId(UUID.fromString(commandDto.getInteractionId())); - - // finally, we execute - commandLogEntryIfAny.ifPresent(commandLogEntry -> - { - commandExecutorService.executeCommand( - CommandExecutorService.InteractionContextPolicy.NO_SWITCH, commandDto); - commandLogEntry.setCompletedAt(clockService.getClock().nowAsJavaSqlTimestamp()); - }); - }) - .ifFailureFail(); + executeCommandWithinOwnTransactionElseFail(commandDto); }) .ifFailure(throwable -> { - log.error("Failed to execute command: " + CommandDtoUtils.dtoMapper().toString(commandDto), throwable); - // update this command as having failed. - interactionService.runAndCatch(interactionContext, () -> { - transactionService.runTransactional(Propagation.REQUIRES_NEW, () -> { - // look up the CommandLogEntry again because we are within a new transaction. - val commandLogEntryIfAny = commandLogEntryRepository.findByInteractionId(UUID.fromString(commandDto.getInteractionId())); - - // capture the error - commandLogEntryIfAny.ifPresent(commandLogEntry -> - { - commandLogEntry.setException(throwable); - commandLogEntry.setCompletedAt(clockService.getClock().nowAsJavaSqlTimestamp()); - }); - }); - }); + logAndCaptureFailure(throwable, commandDto, interactionContext); }); } + private void executeCommandWithinOwnTransactionElseFail(CommandDto commandDto) { + transactionService.runTransactional(Propagation.REQUIRES_NEW, () -> { + // look up the CommandLogEntry again because we are within a new transaction. + val commandLogEntryIfAny = commandLogEntryRepository.findByInteractionId(UUID.fromString(commandDto.getInteractionId())); + + // finally, we execute + commandLogEntryIfAny.ifPresent(commandLogEntry -> + { + commandExecutorService.executeCommand( + CommandExecutorService.InteractionContextPolicy.NO_SWITCH, commandDto); + commandLogEntry.setCompletedAt(clockService.getClock().nowAsJavaSqlTimestamp()); + }); + }) + .ifFailureFail(); + } + + private void logAndCaptureFailure(Throwable throwable, CommandDto commandDto, InteractionContext interactionContext) { + log.error("Failed to execute command: " + CommandDtoUtils.dtoMapper().toString(commandDto), throwable); + // update this command as having failed. + interactionService.runAndCatch(interactionContext, () -> { + transactionService.runTransactional(Propagation.REQUIRES_NEW, () -> { + // look up the CommandLogEntry again because we are within a new transaction. + val commandLogEntryIfAny = commandLogEntryRepository.findByInteractionId(UUID.fromString(commandDto.getInteractionId())); + + // capture the error + commandLogEntryIfAny.ifPresent(commandLogEntry -> + { + commandLogEntry.setException(throwable); + commandLogEntry.setCompletedAt(clockService.getClock().nowAsJavaSqlTimestamp()); + }); + }); + }); + } + }
