Skip to content

Commit 676733c

Browse files
committed
GH-8998: Catch DataIntegrityViolationException instead
Fixes: #8998 Apparently some databases don't throw a proper code to identify a `DuplicateKeyException`, so better to catch `DataIntegrityViolationException` with the same meaning. **Auto-cherry-pick to `6.2.x` & `6.1.x`**
1 parent 3849d6a commit 676733c

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStore.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 the original author or authors.
2+
* Copyright 2017-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323
import org.springframework.beans.factory.InitializingBean;
2424
import org.springframework.context.SmartLifecycle;
2525
import org.springframework.core.log.LogAccessor;
26-
import org.springframework.dao.DuplicateKeyException;
26+
import org.springframework.dao.DataIntegrityViolationException;
2727
import org.springframework.dao.EmptyResultDataAccessException;
2828
import org.springframework.integration.metadata.ConcurrentMetadataStore;
2929
import org.springframework.jdbc.core.JdbcOperations;
@@ -247,7 +247,7 @@ private int tryToPutIfAbsent(String key, String value) {
247247
ps.setString(5, this.region); // NOSONAR magic number
248248
});
249249
}
250-
catch (DuplicateKeyException ex) {
250+
catch (DataIntegrityViolationException ex) {
251251
return 0;
252252
}
253253
}

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcChannelMessageStore.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@
3838
import org.springframework.core.serializer.Deserializer;
3939
import org.springframework.core.serializer.Serializer;
4040
import org.springframework.core.serializer.support.SerializingConverter;
41-
import org.springframework.dao.DuplicateKeyException;
41+
import org.springframework.dao.DataIntegrityViolationException;
4242
import org.springframework.integration.jdbc.store.channel.ChannelMessageStorePreparedStatementSetter;
4343
import org.springframework.integration.jdbc.store.channel.ChannelMessageStoreQueryProvider;
4444
import org.springframework.integration.jdbc.store.channel.MessageRowMapper;
@@ -473,7 +473,7 @@ public MessageGroup addMessageToGroup(Object groupId, final Message<?> message)
473473
ps -> this.preparedStatementSetter.setValues(ps, message, groupId, this.region,
474474
this.priorityEnabled));
475475
}
476-
catch (@SuppressWarnings("unused") DuplicateKeyException e) {
476+
catch (@SuppressWarnings("unused") DataIntegrityViolationException ex) {
477477
LOGGER.debug(() ->
478478
"The Message with id [" + getKey(message.getHeaders().getId()) + "] already exists.\n" +
479479
"Ignoring INSERT...");

spring-integration-mongodb/src/main/java/org/springframework/integration/mongodb/store/AbstractConfigurableMongoDbMessageStore.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@
3030
import org.springframework.beans.factory.InitializingBean;
3131
import org.springframework.context.ApplicationContext;
3232
import org.springframework.context.ApplicationContextAware;
33-
import org.springframework.dao.DuplicateKeyException;
33+
import org.springframework.dao.DataIntegrityViolationException;
3434
import org.springframework.data.domain.Sort;
3535
import org.springframework.data.mongodb.MongoDatabaseFactory;
3636
import org.springframework.data.mongodb.core.FindAndModifyOptions;
@@ -237,7 +237,7 @@ protected void addMessageDocument(final MessageDocument document) {
237237
try {
238238
this.mongoTemplate.insert(document, this.collectionName);
239239
}
240-
catch (DuplicateKeyException e) {
240+
catch (DataIntegrityViolationException e) {
241241
if (this.logger.isDebugEnabled()) {
242242
this.logger.debug("The Message with id [" + document.getMessageId() + "] already exists.\n" +
243243
"Ignoring INSERT and SELECT existing...");

0 commit comments

Comments
 (0)