Search in sources :

Example 16 with CommitTimestampFuture

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

the class RetryOnInvalidatedSessionTest method asyncTransactionManager_readSync.

private void asyncTransactionManager_readSync(final Function<TransactionContext, ResultSet> fn) throws InterruptedException {
    final ExecutorService queryExecutor = Executors.newSingleThreadExecutor();
    try (AsyncTransactionManager manager = client.transactionManagerAsync()) {
        TransactionContextFuture context = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, Long> count = context.then((transaction, ignored) -> {
                    long counter = 0L;
                    try (ResultSet rs = fn.apply(transaction)) {
                        while (rs.next()) {
                            counter++;
                        }
                    }
                    return ApiFutures.immediateFuture(counter);
                }, executor);
                CommitTimestampFuture ts = count.commitAsync();
                assertThrowsSessionNotFoundIfShouldFail(() -> get(ts));
                break;
            } catch (AbortedException e) {
                context = manager.resetForRetryAsync();
            }
        }
    } finally {
        queryExecutor.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)

Example 17 with CommitTimestampFuture

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

the class RetryOnInvalidatedSessionTest method asyncTransactionManager_updateFunction.

private <T> void asyncTransactionManager_updateFunction(final Function<TransactionContext, ApiFuture<T>> fn, T expected) throws InterruptedException {
    try (AsyncTransactionManager manager = client.transactionManagerAsync()) {
        TransactionContextFuture transaction = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, T> res = transaction.then((txn, input) -> fn.apply(txn), executor);
                CommitTimestampFuture ts = res.commitAsync();
                assertThrowsSessionNotFoundIfShouldFail(() -> get(ts));
                break;
            } catch (AbortedException e) {
                transaction = manager.resetForRetryAsync();
            }
        }
    }
}
Also used : TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)

Aggregations

CommitTimestampFuture (com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)17 TransactionContextFuture (com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture)17 ApiFuture (com.google.api.core.ApiFuture)8 ApiFutures (com.google.api.core.ApiFutures)8 AsyncTransactionStep (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep)8 ImmutableList (com.google.common.collect.ImmutableList)8 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)8 Arrays (java.util.Arrays)8 List (java.util.List)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ExecutionException (java.util.concurrent.ExecutionException)8 Test (org.junit.Test)8 ApiFutureCallback (com.google.api.core.ApiFutureCallback)7 SettableApiFuture (com.google.api.core.SettableApiFuture)7 AsyncTransactionFunction (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction)7 SimulatedExecutionTime (com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime)7 StatementResult (com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult)7 INVALID_UPDATE_STATEMENT (com.google.cloud.spanner.MockSpannerTestUtil.INVALID_UPDATE_STATEMENT)7 READ_COLUMN_NAMES (com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES)7 READ_TABLE_NAME (com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME)7