Skip to content

Commit

Permalink
GH-8998: Catch DataIntegrityViolationException instead
Browse files Browse the repository at this point in the history
Fixes: #8998

Apparently some databases don't throw a proper code to identify a `DuplicateKeyException`, so better to catch `DataIntegrityViolationException` with the same meaning.

(cherry picked from commit 676733c)

# Conflicts:
#	spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStore.java
#	spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcChannelMessageStore.java
  • Loading branch information
artembilan committed Mar 12, 2024
1 parent 47be414 commit 306b904
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2023 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@
import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.integration.metadata.ConcurrentMetadataStore;
import org.springframework.jdbc.core.JdbcOperations;
Expand Down Expand Up @@ -191,7 +191,7 @@ private int tryToPutIfAbsent(String key, String value) {
ps.setString(5, this.region); // NOSONAR magic number
});
}
catch (DuplicateKeyException ex) {
catch (DataIntegrityViolationException ex) {
return 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,7 @@
import org.springframework.core.serializer.Deserializer;
import org.springframework.core.serializer.Serializer;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.integration.jdbc.store.channel.ChannelMessageStorePreparedStatementSetter;
import org.springframework.integration.jdbc.store.channel.ChannelMessageStoreQueryProvider;
import org.springframework.integration.jdbc.store.channel.MessageRowMapper;
Expand Down Expand Up @@ -426,7 +426,7 @@ public MessageGroup addMessageToGroup(Object groupId, final Message<?> message)
ps -> this.preparedStatementSetter.setValues(ps, message, groupId, this.region,
this.priorityEnabled));
}
catch (@SuppressWarnings("unused") DuplicateKeyException e) {
catch (@SuppressWarnings("unused") DataIntegrityViolationException ex) {
LOGGER.debug(() ->
"The Message with id [" + getKey(message.getHeaders().getId()) + "] already exists.\n" +
"Ignoring INSERT...");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
Expand Down Expand Up @@ -237,7 +237,7 @@ protected void addMessageDocument(final MessageDocument document) {
try {
this.mongoTemplate.insert(document, this.collectionName);
}
catch (DuplicateKeyException e) {
catch (DataIntegrityViolationException e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("The Message with id [" + document.getMessageId() + "] already exists.\n" +
"Ignoring INSERT and SELECT existing...");
Expand Down

0 comments on commit 306b904

Please sign in to comment.