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