Skip to content

Commit 306b904

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. (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
1 parent 47be414 commit 306b904

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.
@@ -19,7 +19,7 @@
1919
import javax.sql.DataSource;
2020

2121
import org.springframework.beans.factory.InitializingBean;
22-
import org.springframework.dao.DuplicateKeyException;
22+
import org.springframework.dao.DataIntegrityViolationException;
2323
import org.springframework.dao.EmptyResultDataAccessException;
2424
import org.springframework.integration.metadata.ConcurrentMetadataStore;
2525
import org.springframework.jdbc.core.JdbcOperations;
@@ -191,7 +191,7 @@ private int tryToPutIfAbsent(String key, String value) {
191191
ps.setString(5, this.region); // NOSONAR magic number
192192
});
193193
}
194-
catch (DuplicateKeyException ex) {
194+
catch (DataIntegrityViolationException ex) {
195195
return 0;
196196
}
197197
}

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-2022 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.
@@ -36,7 +36,7 @@
3636
import org.springframework.core.serializer.Deserializer;
3737
import org.springframework.core.serializer.Serializer;
3838
import org.springframework.core.serializer.support.SerializingConverter;
39-
import org.springframework.dao.DuplicateKeyException;
39+
import org.springframework.dao.DataIntegrityViolationException;
4040
import org.springframework.integration.jdbc.store.channel.ChannelMessageStorePreparedStatementSetter;
4141
import org.springframework.integration.jdbc.store.channel.ChannelMessageStoreQueryProvider;
4242
import org.springframework.integration.jdbc.store.channel.MessageRowMapper;
@@ -426,7 +426,7 @@ public MessageGroup addMessageToGroup(Object groupId, final Message<?> message)
426426
ps -> this.preparedStatementSetter.setValues(ps, message, groupId, this.region,
427427
this.priorityEnabled));
428428
}
429-
catch (@SuppressWarnings("unused") DuplicateKeyException e) {
429+
catch (@SuppressWarnings("unused") DataIntegrityViolationException ex) {
430430
LOGGER.debug(() ->
431431
"The Message with id [" + getKey(message.getHeaders().getId()) + "] already exists.\n" +
432432
"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)