This is an automated email from the ASF dual-hosted git repository. duanzhengqiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push: new 6ecd87351d9 Fix sonar issue of transaction test (#34213) 6ecd87351d9 is described below commit 6ecd87351d9afcf895ccb14e276009bab953db8b Author: ZhangCheng <chengzh...@apache.org> AuthorDate: Tue Dec 31 09:52:26 2024 +0800 Fix sonar issue of transaction test (#34213) * Fix snoar issue of transaction test * Fix snoar issue of transaction test * Fix sonar issue of transaction test * Add switching transaction rule test case --- .../connection/transaction/TransactionConnectionContext.java | 7 ++++--- .../cases/alterresource/SwitchingTransactionRuleTestCase.java | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/infra/session/src/main/java/org/apache/shardingsphere/infra/session/connection/transaction/TransactionConnectionContext.java b/infra/session/src/main/java/org/apache/shardingsphere/infra/session/connection/transaction/TransactionConnectionContext.java index 67f058db78d..a393a03c969 100644 --- a/infra/session/src/main/java/org/apache/shardingsphere/infra/session/connection/transaction/TransactionConnectionContext.java +++ b/infra/session/src/main/java/org/apache/shardingsphere/infra/session/connection/transaction/TransactionConnectionContext.java @@ -21,6 +21,7 @@ import lombok.Getter; import lombok.Setter; import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; /** * Transaction connection context. @@ -41,7 +42,7 @@ public final class TransactionConnectionContext implements AutoCloseable { @Setter private volatile String readWriteSplitReplicaRoute; - private volatile TransactionManager transactionManager; + private AtomicReference<TransactionManager> transactionManager; /** * Begin transaction. @@ -52,7 +53,7 @@ public final class TransactionConnectionContext implements AutoCloseable { public void beginTransaction(final String transactionType, final TransactionManager transactionManager) { this.transactionType = transactionType; inTransaction = true; - this.transactionManager = transactionManager; + this.transactionManager = new AtomicReference<>(transactionManager); } /** @@ -88,7 +89,7 @@ public final class TransactionConnectionContext implements AutoCloseable { * @return transaction manager */ public Optional<TransactionManager> getTransactionManager() { - return Optional.ofNullable(transactionManager); + return null == transactionManager ? Optional.empty() : Optional.ofNullable(transactionManager.get()); } @Override diff --git a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/SwitchingTransactionRuleTestCase.java b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/SwitchingTransactionRuleTestCase.java index 219cd3b5d0f..1e9bf84b66d 100644 --- a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/SwitchingTransactionRuleTestCase.java +++ b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/SwitchingTransactionRuleTestCase.java @@ -100,18 +100,18 @@ public final class SwitchingTransactionRuleTestCase extends BaseTransactionTestC private final CommonSQLCommand commonSQL; - @SneakyThrows({SQLException.class, InterruptedException.class}) + @SneakyThrows(SQLException.class) @Override public void run() { while (!IS_FINISHED.get()) { alterLocalTransactionRule(); - TimeUnit.SECONDS.sleep(20); + Awaitility.await().atMost(20L, TimeUnit.SECONDS).pollInterval(19L, TimeUnit.SECONDS).until(() -> true); alterXaTransactionRule("Narayana"); if (SWITCH_COUNT.incrementAndGet() >= MAX_SWITCH_COUNT) { IS_FINISHED.set(true); break; } - TimeUnit.SECONDS.sleep(20); + Awaitility.await().atMost(20L, TimeUnit.SECONDS).pollInterval(19L, TimeUnit.SECONDS).until(() -> true); } } @@ -220,7 +220,8 @@ public final class SwitchingTransactionRuleTestCase extends BaseTransactionTestC PreparedStatement deleteStatement = connection.prepareStatement("delete from account where id = ?"); deleteStatement.setObject(1, id); deleteStatement.execute(); - Thread.sleep(random.nextInt(900) + 100); + long time = random.nextLong(900) + 100; + Awaitility.await().atMost(time + 10L, TimeUnit.MILLISECONDS).pollInterval(time, TimeUnit.MILLISECONDS).until(() -> true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON