FINERACT-2096: Fix duplicate key value exception

This commit is contained in:
Jose Alberto Hernandez
2024-06-17 20:46:32 +02:00
committed by Adam Saghy
parent 3b603f5e80
commit edc7fff86f
2 changed files with 17 additions and 2 deletions
@@ -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)
@@ -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());
}
}