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