Search in sources :

Example 6 with AsyncTransactionManager

use of com.google.cloud.spanner.AsyncTransactionManager 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

AbortedException (com.google.cloud.spanner.AbortedException)6 AsyncTransactionManager (com.google.cloud.spanner.AsyncTransactionManager)6 TransactionContextFuture (com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture)6 Test (org.junit.Test)5 ParallelIntegrationTest (com.google.cloud.spanner.ParallelIntegrationTest)4 Struct (com.google.cloud.spanner.Struct)3 ExecutionException (java.util.concurrent.ExecutionException)3 ApiFutures (com.google.api.core.ApiFutures)2 AsyncTransactionStep (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 Key (com.google.cloud.spanner.Key)2 Spanner (com.google.cloud.spanner.Spanner)2 SpannerException (com.google.cloud.spanner.SpannerException)2 ImmutableList (com.google.common.collect.ImmutableList)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)2 Arrays (java.util.Arrays)2 Executors (java.util.concurrent.Executors)2 ApiFunction (com.google.api.core.ApiFunction)1 ApiFuture (com.google.api.core.ApiFuture)1 CommitTimestampFuture (com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)1