Search in sources :

Example 1 with SessionPoolTransactionContext

use of com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManager_shouldRollbackOnCloseAsync.

@Test
public void asyncTransactionManager_shouldRollbackOnCloseAsync() throws Exception {
    AsyncTransactionManager manager = client().transactionManagerAsync();
    TransactionContext txn = manager.beginAsync().get();
    txn.executeUpdateAsync(UPDATE_STATEMENT).get();
    final TransactionSelector selector = ((TransactionContextImpl) ((SessionPoolTransactionContext) txn).delegate).getTransactionSelector();
    SpannerApiFutures.get(manager.closeAsync());
    // The mock server should already have the Rollback request, as we are waiting for the returned
    // ApiFuture to be done.
    mockSpanner.waitForRequestsToContain(input -> {
        if (input instanceof RollbackRequest) {
            RollbackRequest request = (RollbackRequest) input;
            return request.getTransactionId().equals(selector.getId());
        }
        return false;
    }, 0L);
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) SessionPoolTransactionContext(com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext) TransactionSelector(com.google.spanner.v1.TransactionSelector) RollbackRequest(com.google.spanner.v1.RollbackRequest) Test(org.junit.Test)

Example 2 with SessionPoolTransactionContext

use of com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext in project java-spanner by googleapis.

the class MockSpannerServiceImpl method abortTransaction.

/**
 * Instruct the mock server to abort the specified transaction. Use this method to test handling
 * of {@link AbortedException} in your code.
 */
public void abortTransaction(TransactionContext transactionContext) {
    Preconditions.checkNotNull(transactionContext);
    if (transactionContext instanceof SessionPoolTransactionContext) {
        transactionContext = ((SessionPoolTransactionContext) transactionContext).delegate;
    }
    if (transactionContext instanceof TransactionContextImpl) {
        TransactionContextImpl impl = (TransactionContextImpl) transactionContext;
        ByteString id = impl.getTransactionSelector() == null ? null : impl.getTransactionSelector().getId();
        if (id != null) {
            markAbortedTransaction(id);
        }
    } else {
        throw new IllegalArgumentException("Unsupported TransactionContext type: " + transactionContext.getClass().getName());
    }
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) SessionPoolTransactionContext(com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext) ByteString(com.google.protobuf.ByteString)

Aggregations

SessionPoolTransactionContext (com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext)2 TransactionContextImpl (com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl)2 ByteString (com.google.protobuf.ByteString)1 RollbackRequest (com.google.spanner.v1.RollbackRequest)1 TransactionSelector (com.google.spanner.v1.TransactionSelector)1 Test (org.junit.Test)1