Search in sources :

Example 6 with TransactionRunner

use of com.google.cloud.spanner.TransactionRunner in project beam by apache.

the class PartitionMetadataDao method runInTransaction.

/**
 * Runs a given function in a transaction context. The transaction object is given as the
 * parameter to the input function. If the function returns successfully, it will be committed. If
 * the function throws an exception it will be rolled back.
 *
 * @param <T> the return type to be returned from the input transactional function
 * @param callable the function to be executed within the transaction context
 * @return a transaction result containing the result from the function and a commit timestamp for
 *     the read / write transaction
 */
public <T> TransactionResult<T> runInTransaction(Function<InTransactionContext, T> callable) {
    final TransactionRunner readWriteTransaction = databaseClient.readWriteTransaction();
    final T result = readWriteTransaction.run(transaction -> {
        final InTransactionContext transactionContext = new InTransactionContext(metadataTableName, transaction);
        return callable.apply(transactionContext);
    });
    return new TransactionResult<>(result, readWriteTransaction.getCommitTimestamp());
}
Also used : COLUMN_SCHEDULED_AT(org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_SCHEDULED_AT) COLUMN_FINISHED_AT(org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_FINISHED_AT) COLUMN_CREATED_AT(org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_CREATED_AT) COLUMN_RUNNING_AT(org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_RUNNING_AT) TransactionRunner(com.google.cloud.spanner.TransactionRunner)

Aggregations

TransactionRunner (com.google.cloud.spanner.TransactionRunner)6 TransactionContext (com.google.cloud.spanner.TransactionContext)5 SpannerException (com.google.cloud.spanner.SpannerException)3 Database (com.google.cloud.spanner.Database)2 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 Mutation (com.google.cloud.spanner.Mutation)2 Struct (com.google.cloud.spanner.Struct)2 TransactionCallable (com.google.cloud.spanner.TransactionRunner.TransactionCallable)2 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)2 ArrayList (java.util.ArrayList)2 Nullable (javax.annotation.Nullable)2 Before (org.junit.Before)2 Timestamp (com.google.cloud.Timestamp)1 AbortedException (com.google.cloud.spanner.AbortedException)1 ResultSet (com.google.cloud.spanner.ResultSet)1 Statement (com.google.cloud.spanner.Statement)1 SQLException (java.sql.SQLException)1 Vector (java.util.Vector)1 CountDownLatch (java.util.concurrent.CountDownLatch)1