diff --git a/fineract-core/src/main/java/org/apache/fineract/commands/service/CommandSourceService.java b/fineract-core/src/main/java/org/apache/fineract/commands/service/CommandSourceService.java index cb5dfaca0..46432d613 100644 --- a/fineract-core/src/main/java/org/apache/fineract/commands/service/CommandSourceService.java +++ b/fineract-core/src/main/java/org/apache/fineract/commands/service/CommandSourceService.java @@ -31,8 +31,10 @@ import org.apache.fineract.commands.handler.NewCommandSourceHandler; import org.apache.fineract.infrastructure.core.api.JsonCommand; import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; import org.apache.fineract.infrastructure.core.exception.ErrorHandler; +import org.apache.fineract.infrastructure.core.exception.IdempotentCommandProcessUnderProcessingException; import org.apache.fineract.useradministration.domain.AppUser; import org.jetbrains.annotations.NotNull; +import org.springframework.orm.jpa.JpaSystemException; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; @@ -64,8 +66,16 @@ public class CommandSourceService { @NotNull private CommandSource saveInitial(CommandWrapper wrapper, JsonCommand jsonCommand, AppUser maker, String idempotencyKey) { - CommandSource initialCommandSource = getInitialCommandSource(wrapper, jsonCommand, maker, idempotencyKey); - return commandSourceRepository.saveAndFlush(initialCommandSource); + try { + CommandSource initialCommandSource = getInitialCommandSource(wrapper, jsonCommand, maker, idempotencyKey); + return commandSourceRepository.saveAndFlush(initialCommandSource); + } catch (JpaSystemException jse) { + final String message = (jse.getRootCause() != null) ? jse.getRootCause().getMessage() : null; + if (message != null && message.toUpperCase().contains("UNIQUE_PORTFOLIO_COMMAND_SOURCE")) { + throw new IdempotentCommandProcessUnderProcessingException(wrapper, idempotencyKey, jse); + } + throw jse; + } } @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.REPEATABLE_READ) diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/exception/IdempotentCommandProcessUnderProcessingException.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/exception/IdempotentCommandProcessUnderProcessingException.java index 7e608e4be..ba01dedac 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/exception/IdempotentCommandProcessUnderProcessingException.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/exception/IdempotentCommandProcessUnderProcessingException.java @@ -28,4 +28,9 @@ public class IdempotentCommandProcessUnderProcessingException extends AbstractId public IdempotentCommandProcessUnderProcessingException(CommandWrapper wrapper, String idempotencyKey) { super(wrapper.actionName(), wrapper.entityName(), idempotencyKey, wrapper.getJson()); } + + public IdempotentCommandProcessUnderProcessingException(CommandWrapper wrapper, String idempotencyKey, Exception e) { + super(wrapper.actionName(), wrapper.entityName(), idempotencyKey, wrapper.getJson()); + } + }