Skip to content

Commit

Permalink
Fix sonar issue of transaction test (#34213)
Browse files Browse the repository at this point in the history
* Fix snoar issue of transaction test

* Fix snoar issue of transaction test

* Fix sonar issue of transaction test

* Add switching transaction rule test case
  • Loading branch information
FlyingZC authored Dec 31, 2024
1 parent 8e52107 commit 6ecd873
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Setter;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/**
* Transaction connection context.
Expand All @@ -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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -88,7 +89,7 @@ public Optional<String> getReadWriteSplitReplicaRoute() {
* @return transaction manager
*/
public Optional<TransactionManager> getTransactionManager() {
return Optional.ofNullable(transactionManager);
return null == transactionManager ? Optional.empty() : Optional.ofNullable(transactionManager.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,18 @@ private static class AlterTransactionRuleTask implements Runnable {

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);
}
}

Expand Down Expand Up @@ -220,7 +220,8 @@ private static void executeOneTransaction(final Connection connection) throws SQ
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
Expand Down

0 comments on commit 6ecd873

Please sign in to comment.