GH-3282: Fix JdbcMetadataStore for DuplicateKeyEx (#3285)

* GH-3282: Fix JdbcMetadataStore for DuplicateKeyEx

Fixes: https://github.com/spring-projects/spring-integration/issues/3282

The `INSERT INTO ... SELECT ... FROM ... HAVING` may fail with
`DuplicateKeyException` in between transactions.
* Catch `DuplicateKeyException` from in the `tryToPutIfAbsent()`
and return `0` as a fact of not inserted to let other logic to work
as expected.

There is no test coverage for this since it is almost impossible to
reproduce such a race condition on DB

**Cherry-pick to 5.3.x & 5.2.x**

* * Remove misleading message in the comment sentence
This commit is contained in:
Artem Bilan
2020-05-20 14:45:45 -04:00
committed by GitHub
parent 75021beae4
commit 665f6bfb69
2 changed files with 29 additions and 22 deletions

View File

@@ -20,14 +20,12 @@ import static org.assertj.core.api.Assertions.assertThat;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -36,8 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
*
* @since 5.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext // close at the end after class
@Transactional
public class JdbcMetadataStoreTests {
@@ -48,7 +45,7 @@ public class JdbcMetadataStoreTests {
private JdbcMetadataStore metadataStore;
@Before
@BeforeEach
public void init() {
metadataStore = new JdbcMetadataStore(dataSource);
metadataStore.afterPropertiesSet();