-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-9481: Fix
JdbcMetadataStore
for PostgreSQL & MySQL
Fixes: #9481 Issue link: #9481 MySQL throws `CannotAcquireLockException` in case of duplicate key failure. PostgreSQL just rollbacks a transaction not letting us move on with a `SELECT` * Include `TransientDataAccessException` to the catch block of the `INSERT` to ignore it for the subsequent `SELECT` * Add logic to determine `PostgreSQL` database vendor and include `ON CONFLICT DO NOTHING` hint into the `INSERT` to not fail in case of duplicate key found on `putIfAbsent` operation (cherry picked from commit 98d0266)
- Loading branch information
1 parent
d302804
commit 229ffb7
Showing
6 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...dbc/src/test/java/org/springframework/integration/jdbc/mysql/MySqlMetadataStoreTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.integration.jdbc.mysql; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.integration.jdbc.metadata.JdbcMetadataStore; | ||
import org.springframework.integration.metadata.ConcurrentMetadataStore; | ||
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
import org.springframework.jdbc.datasource.init.DataSourceInitializer; | ||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Artem Bilan | ||
* | ||
* @since 6.4 | ||
*/ | ||
@SpringJUnitConfig | ||
@DirtiesContext | ||
class MySqlMetadataStoreTests implements MySqlContainerTest { | ||
|
||
@Autowired | ||
ConcurrentMetadataStore jdbcMetadataStore; | ||
|
||
@Test | ||
void verifyJdbcMetadataStoreConcurrency() throws InterruptedException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(100); | ||
CountDownLatch successPutIfAbsents = new CountDownLatch(100); | ||
for (int i = 0; i < 100; i++) { | ||
executorService.execute(() -> { | ||
this.jdbcMetadataStore.putIfAbsent("testKey", "testValue"); | ||
successPutIfAbsents.countDown(); | ||
}); | ||
} | ||
assertThat(successPutIfAbsents.await(10, TimeUnit.SECONDS)).isTrue(); | ||
executorService.shutdown(); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@EnableTransactionManagement | ||
static class TestConfiguration { | ||
|
||
@Value("org/springframework/integration/jdbc/schema-mysql.sql") | ||
Resource createSchemaScript; | ||
|
||
@Bean | ||
DataSource dataSource() { | ||
return MySqlContainerTest.dataSource(); | ||
} | ||
|
||
@Bean | ||
DataSourceInitializer dataSourceInitializer(DataSource dataSource) { | ||
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer(); | ||
dataSourceInitializer.setDataSource(dataSource); | ||
dataSourceInitializer.setDatabasePopulator(new ResourceDatabasePopulator(this.createSchemaScript)); | ||
return dataSourceInitializer; | ||
} | ||
|
||
@Bean | ||
PlatformTransactionManager transactionManager(DataSource dataSource) { | ||
return new DataSourceTransactionManager(dataSource); | ||
} | ||
|
||
@Bean | ||
JdbcMetadataStore jdbcMetadataStore(DataSource dataSource) { | ||
return new JdbcMetadataStore(dataSource); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...c/test/java/org/springframework/integration/jdbc/postgres/PostgresMetadataStoreTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.integration.jdbc.postgres; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.apache.commons.dbcp2.BasicDataSource; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.integration.jdbc.metadata.JdbcMetadataStore; | ||
import org.springframework.integration.metadata.ConcurrentMetadataStore; | ||
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Artem Bilan | ||
* | ||
* @since 6.2.9 | ||
*/ | ||
@SpringJUnitConfig | ||
@DirtiesContext | ||
class PostgresMetadataStoreTests implements PostgresContainerTest { | ||
|
||
@Autowired | ||
ConcurrentMetadataStore jdbcMetadataStore; | ||
|
||
@Test | ||
void verifyJdbcMetadataStoreConcurrency() throws InterruptedException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(100); | ||
CountDownLatch successPutIfAbsents = new CountDownLatch(100); | ||
for (int i = 0; i < 100; i++) { | ||
executorService.execute(() -> { | ||
this.jdbcMetadataStore.putIfAbsent("testKey", "testValue"); | ||
successPutIfAbsents.countDown(); | ||
}); | ||
} | ||
assertThat(successPutIfAbsents.await(10, TimeUnit.SECONDS)).isTrue(); | ||
executorService.shutdown(); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@EnableTransactionManagement | ||
static class TestConfiguration { | ||
|
||
@Bean | ||
DataSource dataSource() { | ||
BasicDataSource dataSource = new BasicDataSource(); | ||
dataSource.setUrl(PostgresContainerTest.getJdbcUrl()); | ||
dataSource.setUsername(PostgresContainerTest.getUsername()); | ||
dataSource.setPassword(PostgresContainerTest.getPassword()); | ||
return dataSource; | ||
} | ||
|
||
@Bean | ||
PlatformTransactionManager transactionManager(DataSource dataSource) { | ||
return new DataSourceTransactionManager(dataSource); | ||
} | ||
|
||
@Bean | ||
JdbcMetadataStore jdbcMetadataStore(DataSource dataSource) { | ||
return new JdbcMetadataStore(dataSource); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters