Search in sources :

Example 36 with TransactionContextFuture

use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.

the class ITTransactionManagerAsyncTest method testRollback.

@Test
public void testRollback() throws InterruptedException {
    try (AsyncTransactionManager manager = client.transactionManagerAsync()) {
        TransactionContextFuture txn = manager.beginAsync();
        while (true) {
            txn.then((transaction, ignored) -> {
                transaction.buffer(Mutation.newInsertBuilder("T").set("K").to("Key2").set("BoolValue").to(true).build());
                return ApiFutures.immediateFuture(null);
            }, executor);
            try {
                manager.rollbackAsync();
                break;
            } catch (AbortedException e) {
                Thread.sleep(e.getRetryDelayInMillis());
                txn = manager.resetForRetryAsync();
            }
        }
        assertThat(manager.getState()).isEqualTo(TransactionState.ROLLED_BACK);
        // Row should not have been inserted.
        assertThat(client.singleUse().readRow("T", Key.of("Key2"), Arrays.asList("K", "BoolValue"))).isNull();
    }
}
Also used : AsyncTransactionManager(com.google.cloud.spanner.AsyncTransactionManager) AbortedException(com.google.cloud.spanner.AbortedException) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Example 37 with TransactionContextFuture

use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.

the class ITTransactionManagerAsyncTest method testSimpleInsert.

@Test
public void testSimpleInsert() throws ExecutionException, InterruptedException {
    try (AsyncTransactionManager manager = client.transactionManagerAsync()) {
        TransactionContextFuture txn = manager.beginAsync();
        while (true) {
            assertThat(manager.getState()).isEqualTo(TransactionState.STARTED);
            try {
                txn.then((transaction, ignored) -> {
                    transaction.buffer(Mutation.newInsertBuilder("T").set("K").to("Key1").set("BoolValue").to(true).build());
                    return ApiFutures.immediateFuture(null);
                }, executor).commitAsync().get();
                assertThat(manager.getState()).isEqualTo(TransactionState.COMMITTED);
                Struct row = client.singleUse().readRow("T", Key.of("Key1"), Arrays.asList("K", "BoolValue"));
                assertThat(row.getString(0)).isEqualTo("Key1");
                assertThat(row.getBoolean(1)).isTrue();
                break;
            } catch (AbortedException e) {
                Thread.sleep(e.getRetryDelayInMillis());
                txn = manager.resetForRetryAsync();
            }
        }
    }
}
Also used : AsyncTransactionManager(com.google.cloud.spanner.AsyncTransactionManager) AbortedException(com.google.cloud.spanner.AbortedException) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) Struct(com.google.cloud.spanner.Struct) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Aggregations

TransactionContextFuture (com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture)37 Test (org.junit.Test)32 CommitTimestampFuture (com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 ExecutionException (java.util.concurrent.ExecutionException)16 ApiFutures (com.google.api.core.ApiFutures)15 AsyncTransactionStep (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep)15 ImmutableList (com.google.common.collect.ImmutableList)15 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)15 CommitRequest (com.google.spanner.v1.CommitRequest)15 Arrays (java.util.Arrays)15 ApiFuture (com.google.api.core.ApiFuture)14 Truth.assertThat (com.google.common.truth.Truth.assertThat)14 Collection (java.util.Collection)14 Collections (java.util.Collections)14 ApiFutureCallback (com.google.api.core.ApiFutureCallback)13 SettableApiFuture (com.google.api.core.SettableApiFuture)13 AsyncTransactionFunction (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction)13 SimulatedExecutionTime (com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime)13 StatementResult (com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult)13