|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2025 Google LLC | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + *      https://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | + | 
|  | 17 | +package com.google.cloud.spanner; | 
|  | 18 | + | 
|  | 19 | +import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator; | 
|  | 20 | +import static com.google.common.truth.Truth.assertThat; | 
|  | 21 | +import static org.junit.Assert.assertTrue; | 
|  | 22 | +import static org.junit.Assume.assumeFalse; | 
|  | 23 | + | 
|  | 24 | +import org.junit.ClassRule; | 
|  | 25 | +import org.junit.Test; | 
|  | 26 | +import org.junit.experimental.categories.Category; | 
|  | 27 | +import org.junit.runner.RunWith; | 
|  | 28 | +import org.junit.runners.JUnit4; | 
|  | 29 | + | 
|  | 30 | +@Category(ParallelIntegrationTest.class) | 
|  | 31 | +@RunWith(JUnit4.class) | 
|  | 32 | +public class ITTransactionRetryTest { | 
|  | 33 | + | 
|  | 34 | +  @ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv(); | 
|  | 35 | + | 
|  | 36 | +  @Test | 
|  | 37 | +  public void TestRetryInfo() { | 
|  | 38 | +    assumeFalse("emulator does not support parallel transaction", isUsingEmulator()); | 
|  | 39 | + | 
|  | 40 | +    // Creating a database with the table which contains INT64 columns | 
|  | 41 | +    Database db = | 
|  | 42 | +        env.getTestHelper() | 
|  | 43 | +            .createTestDatabase("CREATE TABLE Test(ID INT64, " + "EMPID INT64) PRIMARY KEY (ID)"); | 
|  | 44 | +    DatabaseClient databaseClient = env.getTestHelper().getClient().getDatabaseClient(db.getId()); | 
|  | 45 | + | 
|  | 46 | +    // Inserting one row | 
|  | 47 | +    databaseClient | 
|  | 48 | +        .readWriteTransaction() | 
|  | 49 | +        .run( | 
|  | 50 | +            transaction -> { | 
|  | 51 | +              transaction.buffer( | 
|  | 52 | +                  Mutation.newInsertBuilder("Test").set("ID").to(1).set("EMPID").to(1).build()); | 
|  | 53 | +              return null; | 
|  | 54 | +            }); | 
|  | 55 | + | 
|  | 56 | +    int numRetries = 10; | 
|  | 57 | +    boolean isAbortedWithRetryInfo = false; | 
|  | 58 | +    while (numRetries-- > 0) { | 
|  | 59 | +      try (TransactionManager transactionManager1 = databaseClient.transactionManager()) { | 
|  | 60 | +        try (TransactionManager transactionManager2 = databaseClient.transactionManager()) { | 
|  | 61 | +          try { | 
|  | 62 | +            TransactionContext transaction1 = transactionManager1.begin(); | 
|  | 63 | +            TransactionContext transaction2 = transactionManager2.begin(); | 
|  | 64 | +            transaction1.executeUpdate( | 
|  | 65 | +                Statement.of("UPDATE Test SET EMPID = EMPID + 1 WHERE ID = 1")); | 
|  | 66 | +            transaction2.executeUpdate( | 
|  | 67 | +                Statement.of("UPDATE Test SET EMPID = EMPID + 1 WHERE ID = 1")); | 
|  | 68 | +            transactionManager1.commit(); | 
|  | 69 | +            transactionManager2.commit(); | 
|  | 70 | +          } catch (AbortedException abortedException) { | 
|  | 71 | +            assertThat(abortedException.getErrorCode()).isEqualTo(ErrorCode.ABORTED); | 
|  | 72 | +            if (abortedException.getRetryDelayInMillis() > 0) { | 
|  | 73 | +              isAbortedWithRetryInfo = true; | 
|  | 74 | +              break; | 
|  | 75 | +            } | 
|  | 76 | +          } | 
|  | 77 | +        } | 
|  | 78 | +      } | 
|  | 79 | +    } | 
|  | 80 | + | 
|  | 81 | +    assertTrue("Transaction is not aborted with the trailers", isAbortedWithRetryInfo); | 
|  | 82 | +  } | 
|  | 83 | +} | 
0 commit comments