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