You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by hu...@apache.org on 2026/08/13 06:47:36 UTC
(shardingsphere) branch master updated: Fix classic transfer transaction E2E case (#39434)
This is an automated email from the ASF dual-hosted git repository.
iamhucong 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 6d0fd630540 Fix classic transfer transaction E2E case (#39434)
6d0fd630540 is described below
commit 6d0fd6305403849f4a7b298846b87e1d30319be5
Author: ZhangCheng <ch...@apache.org>
AuthorDate: Thu Aug 13 14:47:31 2026 +0800
Fix classic transfer transaction E2E case (#39434)
---
.../classictransfer/ClassicTransferTestCase.java | 80 ++++++++++++++--------
.../src/test/resources/env/e2e-env.properties | 2 +-
2 files changed, 52 insertions(+), 30 deletions(-)
diff --git a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/operation/transaction/cases/classictransfer/ClassicTransferTestCase.java b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/operation/transaction/cases/classictransfer/ClassicTransferTestCase.java
index 0c259ccbd8d..42ffabdc510 100644
--- a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/operation/transaction/cases/classictransfer/ClassicTransferTestCase.java
+++ b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/operation/transaction/cases/classictransfer/ClassicTransferTestCase.java
@@ -17,25 +17,24 @@
package org.apache.shardingsphere.test.e2e.operation.transaction.cases.classictransfer;
-import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
import org.apache.shardingsphere.test.e2e.operation.transaction.cases.base.BaseTransactionTestCase;
import org.apache.shardingsphere.test.e2e.operation.transaction.engine.base.TransactionContainerComposer;
import org.apache.shardingsphere.test.e2e.operation.transaction.engine.base.TransactionTestCase;
-import org.awaitility.Awaitility;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.Collection;
import java.util.LinkedList;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Classic transfer transaction integration test.
@@ -50,60 +49,83 @@ public final class ClassicTransferTestCase extends BaseTransactionTestCase {
@Override
public void executeTest(final TransactionContainerComposer containerComposer) throws SQLException {
try (Connection connection = getDataSource().getConnection()) {
- executeUpdateWithLog(connection, "INSERT INTO account(transaction_id, balance) VALUES (1,0), (2,100);");
+ executeUpdateWithLog(connection, "INSERT INTO account(id, transaction_id, balance) VALUES (1, 1, 0), (2, 2, 100);");
}
innerRun();
}
- @SneakyThrows(InterruptedException.class)
private void innerRun() throws SQLException {
- List<Thread> tasks = new LinkedList<>();
+ Collection<Thread> tasks = new LinkedList<>();
+ AtomicReference<Throwable> failure = new AtomicReference<>();
for (int i = 0; i < 20; i++) {
- Thread updateThread = new Thread(new UpdateAccountTask(getDataSource()));
+ Thread updateThread = new Thread(new UpdateAccountTask(getDataSource(), failure));
updateThread.start();
tasks.add(updateThread);
- int sum = getBalanceSum();
- assertThat(String.format("Balance sum is %s, should be 100.", sum), sum, is(100));
}
- Awaitility.await().pollDelay(3L, TimeUnit.SECONDS).until(() -> true);
- int sum = getBalanceSum();
- assertThat(String.format("Balance sum is %s, should be 100.", sum), sum, is(100));
for (Thread task : tasks) {
- task.join();
+ try {
+ task.join();
+ } catch (final InterruptedException ex) {
+ Thread.currentThread().interrupt();
+ throw new SQLException("Interrupted while waiting for transfer tasks.", ex);
+ }
+ }
+ Throwable actualFailure = failure.get();
+ if (actualFailure instanceof SQLException) {
+ throw (SQLException) actualFailure;
}
+ if (null != actualFailure) {
+ throw (AssertionError) actualFailure;
+ }
+ assertAccountBalances();
}
- private int getBalanceSum() throws SQLException {
- int result = 0;
+ private void assertAccountBalances() throws SQLException {
try (
Connection connection = getDataSource().getConnection();
Statement statement = connection.createStatement()) {
connection.setAutoCommit(false);
- ResultSet resultSet = statement.executeQuery("SELECT SUM(balance) AS a FROM account WHERE transaction_id IN (1, 2)");
- if (resultSet.next()) {
- result = resultSet.getInt(1);
+ try (ResultSet resultSet = statement.executeQuery("SELECT transaction_id, balance FROM account WHERE transaction_id IN (1, 2) ORDER BY transaction_id")) {
+ assertTrue(resultSet.next());
+ assertThat(resultSet.getInt("transaction_id"), is(1));
+ assertThat(resultSet.getInt("balance"), is(20));
+ assertTrue(resultSet.next());
+ assertThat(resultSet.getInt("transaction_id"), is(2));
+ assertThat(resultSet.getInt("balance"), is(80));
+ assertFalse(resultSet.next());
}
connection.commit();
}
- return result;
}
@RequiredArgsConstructor
- @Getter
private static final class UpdateAccountTask implements Runnable {
private final DataSource dataSource;
+ private final AtomicReference<Throwable> failure;
+
public void run() {
try (Connection connection = dataSource.getConnection()) {
connection.setAutoCommit(false);
- Statement statement1 = connection.createStatement();
- statement1.execute("UPDATE account SET balance = balance - 1 WHERE transaction_id = 2;");
- Statement statement2 = connection.createStatement();
- Awaitility.await().pollDelay(1L, TimeUnit.SECONDS).until(() -> true);
- statement2.execute("UPDATE account SET balance = balance + 1 WHERE transaction_id = 1;");
- connection.commit();
- } catch (final SQLException ignored) {
+ try (Statement statement = connection.createStatement()) {
+ assertThat(statement.executeUpdate("UPDATE account SET balance = balance - 1 WHERE id = 2 AND transaction_id = 2;"), is(1));
+ assertThat(statement.executeUpdate("UPDATE account SET balance = balance + 1 WHERE id = 1 AND transaction_id = 1;"), is(1));
+ connection.commit();
+ } catch (final SQLException | AssertionError ex) {
+ rollback(connection, ex);
+ throw ex;
+ }
+ } catch (final SQLException | AssertionError ex) {
+ failure.compareAndSet(null, ex);
+ }
+ }
+
+ private void rollback(final Connection connection, final Throwable cause) {
+ try {
+ connection.rollback();
+ } catch (final SQLException ex) {
+ cause.addSuppressed(ex);
}
}
}
diff --git a/test/e2e/operation/transaction/src/test/resources/env/e2e-env.properties b/test/e2e/operation/transaction/src/test/resources/env/e2e-env.properties
index 187926ee2a8..6c473dd6a41 100644
--- a/test/e2e/operation/transaction/src/test/resources/env/e2e-env.properties
+++ b/test/e2e/operation/transaction/src/test/resources/env/e2e-env.properties
@@ -21,7 +21,7 @@ e2e.scenarios=default,cursor,readwrite_splitting
e2e.run.type=
#e2e.transaction.cases=ClassicTransferTestCase
-e2e.transaction.cases=TransactionDeadlockTestCase, MultiJDBCConnectionsTestCase, MultiTransactionInConnectionTestCase, MultiOperationsCommitAndRollbackTestCase, MySQLAutoCommitTestCase, PostgreSQLAutoCommitTestCase, BroadcastTableTransactionTestCase, ExceptionInTransactionTestCase, MultiTableCommitAndRollbackTestCase, SingleTableCommitAndRollbackTestCase, MySQLSetReadOnlyTestCase, MySQLLocalTruncateTestCase, MySQLXATruncateTestCase, OpenGaussCursorTestCase, NestedTransactionTestCase, Rea [...]
+e2e.transaction.cases=ClassicTransferTestCase, TransactionDeadlockTestCase, MultiJDBCConnectionsTestCase, MultiTransactionInConnectionTestCase, MultiOperationsCommitAndRollbackTestCase, MySQLAutoCommitTestCase, PostgreSQLAutoCommitTestCase, BroadcastTableTransactionTestCase, ExceptionInTransactionTestCase, MultiTableCommitAndRollbackTestCase, SingleTableCommitAndRollbackTestCase, MySQLSetReadOnlyTestCase, MySQLLocalTruncateTestCase, MySQLXATruncateTestCase, OpenGaussCursorTestCase, Neste [...]
#e2e.transaction.types=LOCAL,XA,BASE
e2e.transaction.types=LOCAL,XA