use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerUpdate.
@Test
public void asyncTransactionManagerUpdate() throws Exception {
try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
AsyncTransactionStep<Void, Long> updateCount = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.executeUpdateAsync(UPDATE_STATEMENT), executor);
CommitTimestampFuture commitTimestamp = updateCount.commitAsync();
assertThat(updateCount.get()).isEqualTo(UPDATE_COUNT);
assertThat(commitTimestamp.get()).isNotNull();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerWaitsUntilAsyncUpdateHasFinished.
@Test
public void asyncTransactionManagerWaitsUntilAsyncUpdateHasFinished() throws Exception {
try (AsyncTransactionManager mgr = clientWithEmptySessionPool().transactionManagerAsync()) {
TransactionContextFuture txn = mgr.beginAsync();
while (true) {
try {
txn.then((transaction, input) -> {
// Shoot-and-forget update. The commit will still wait for this request to
// finish.
transaction.executeUpdateAsync(UPDATE_STATEMENT);
return ApiFutures.immediateFuture(null);
}, executor).commitAsync().get();
assertThat(mockSpanner.getRequestTypes()).containsExactly(BatchCreateSessionsRequest.class, ExecuteSqlRequest.class, CommitRequest.class);
break;
} catch (AbortedException e) {
txn = mgr.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerBatchUpdate.
@Test
public void asyncTransactionManagerBatchUpdate() throws Exception {
try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
AsyncTransactionStep<Void, long[]> updateCounts = transactionContextFuture.then((transaction, ignored) -> transaction.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT)), executor);
get(updateCounts.commitAsync());
assertThat(get(updateCounts)).asList().containsExactly(UPDATE_COUNT, UPDATE_COUNT);
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerQuery.
@Test
public void asyncTransactionManagerQuery() throws Exception {
mockSpanner.putStatementResult(StatementResult.query(Statement.of("SELECT FirstName FROM Singers WHERE ID=1"), MockSpannerTestUtil.READ_FIRST_NAME_SINGERS_RESULTSET));
final long singerId = 1L;
try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
final String column = "FirstName";
CommitTimestampFuture commitTimestamp = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.readRowAsync("Singers", Key.of(singerId), Collections.singleton(column)), executor).then((transaction, input) -> {
String name = input.getString(column);
return transaction.bufferAsync(Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
}, executor).commitAsync();
try {
commitTimestamp.get();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture in project java-spanner by googleapis.
the class AsyncTransactionManagerTest method asyncTransactionManagerIsNonBlocking.
@Test
public void asyncTransactionManagerIsNonBlocking() throws Exception {
mockSpanner.freeze();
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
TransactionContextFuture transactionContextFuture = manager.beginAsync();
while (true) {
try {
AsyncTransactionStep<Void, Long> updateCount = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.executeUpdateAsync(UPDATE_STATEMENT), executor);
CommitTimestampFuture commitTimestamp = updateCount.commitAsync();
mockSpanner.unfreeze();
assertThat(updateCount.get(10L, TimeUnit.SECONDS)).isEqualTo(UPDATE_COUNT);
assertThat(commitTimestamp.get(10L, TimeUnit.SECONDS)).isNotNull();
break;
} catch (AbortedException e) {
transactionContextFuture = manager.resetForRetryAsync();
}
}
}
}
Aggregations