Search in sources :

Example 16 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project spring-cloud-gcp by GoogleCloudPlatform.

the class ReactiveFirestoreTransactionManager method startTransaction.

private Mono<ReactiveFirestoreResourceHolder> startTransaction(TransactionDefinition definition) {
    TransactionOptions.Builder txOptions = definition.isReadOnly() ? TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.newBuilder().build()) : TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.newBuilder().build());
    BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setOptions(txOptions).setDatabase(this.databasePath).build();
    return ObservableReactiveUtil.<BeginTransactionResponse>unaryCall(obs -> this.firestore.beginTransaction(beginTransactionRequest, obs)).map(beginTransactionResponse -> new ReactiveFirestoreResourceHolder(beginTransactionResponse.getTransaction()));
}
Also used : CommitResponse(com.google.firestore.v1.CommitResponse) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) Util(com.google.cloud.spring.data.firestore.util.Util) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Mono(reactor.core.publisher.Mono) Timestamp(com.google.cloud.Timestamp) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest) AbstractReactiveTransactionManager(org.springframework.transaction.reactive.AbstractReactiveTransactionManager) ByteString(com.google.protobuf.ByteString) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) SmartTransactionObject(org.springframework.transaction.support.SmartTransactionObject) TransactionSystemException(org.springframework.transaction.TransactionSystemException) GenericReactiveTransaction(org.springframework.transaction.reactive.GenericReactiveTransaction) CommitRequest(com.google.firestore.v1.CommitRequest) TransactionOptions(com.google.firestore.v1.TransactionOptions) RollbackRequest(com.google.firestore.v1.RollbackRequest) TransactionException(org.springframework.transaction.TransactionException) FirestoreStub(com.google.firestore.v1.FirestoreGrpc.FirestoreStub) Nullable(org.springframework.lang.Nullable) FirestoreClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreClassMapper) ObservableReactiveUtil(com.google.cloud.spring.data.firestore.util.ObservableReactiveUtil) Assert(org.springframework.util.Assert) TransactionOptions(com.google.firestore.v1.TransactionOptions) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest)

Example 17 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project java-spanner by googleapis.

the class SessionImpl method beginTransactionAsync.

ApiFuture<ByteString> beginTransactionAsync() {
    final SettableApiFuture<ByteString> res = SettableApiFuture.create();
    final Span span = tracer.spanBuilder(SpannerImpl.BEGIN_TRANSACTION).startSpan();
    final BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(name).setOptions(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance())).build();
    final ApiFuture<Transaction> requestFuture = spanner.getRpc().beginTransactionAsync(request, options);
    requestFuture.addListener(tracer.withSpan(span, () -> {
        try {
            Transaction txn = requestFuture.get();
            if (txn.getId().isEmpty()) {
                throw newSpannerException(ErrorCode.INTERNAL, "Missing id in transaction\n" + getName());
            }
            span.end(TraceUtil.END_SPAN_OPTIONS);
            res.set(txn.getId());
        } catch (ExecutionException e) {
            TraceUtil.endSpanWithFailure(span, e);
            res.setException(SpannerExceptionFactory.newSpannerException(e.getCause() == null ? e : e.getCause()));
        } catch (InterruptedException e) {
            TraceUtil.endSpanWithFailure(span, e);
            res.setException(SpannerExceptionFactory.propagateInterrupt(e));
        } catch (Exception e) {
            TraceUtil.endSpanWithFailure(span, e);
            res.setException(e);
        }
    }), MoreExecutors.directExecutor());
    return res;
}
Also used : SingleUseReadOnlyTransaction(com.google.cloud.spanner.AbstractReadContext.SingleUseReadOnlyTransaction) Transaction(com.google.spanner.v1.Transaction) MultiUseReadOnlyTransaction(com.google.cloud.spanner.AbstractReadContext.MultiUseReadOnlyTransaction) ByteString(com.google.protobuf.ByteString) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) ExecutionException(java.util.concurrent.ExecutionException) Span(io.opencensus.trace.Span) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) ExecutionException(java.util.concurrent.ExecutionException)

Example 18 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project java-firestore by googleapis.

the class FirestoreClientTest method beginTransactionTest.

@Test
public void beginTransactionTest() throws Exception {
    BeginTransactionResponse expectedResponse = BeginTransactionResponse.newBuilder().setTransaction(ByteString.EMPTY).build();
    mockFirestore.addResponse(expectedResponse);
    String database = "database1789464955";
    BeginTransactionResponse actualResponse = client.beginTransaction(database);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockFirestore.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BeginTransactionRequest actualRequest = ((BeginTransactionRequest) actualRequests.get(0));
    Assert.assertEquals(database, actualRequest.getDatabase());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) BeginTransactionRequest(com.google.firestore.v1.BeginTransactionRequest) ByteString(com.google.protobuf.ByteString) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Test(org.junit.Test)

Example 19 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ClientImplTest method runAsyncStackTraceCaptureTest.

private void runAsyncStackTraceCaptureTest(boolean asyncStackTraceCaptureEnabled) throws Exception {
    DatastoreServiceGlobalConfig.setConfig(DatastoreServiceGlobalConfig.builder().appId(APP_ID).emulatorHost("dummy-value-to-stub-out-credentials").asyncStackTraceCaptureEnabled(asyncStackTraceCaptureEnabled).build());
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 1);
    DatastoreException lastException = nonRetryableException();
    when(datastore.beginTransaction(req)).thenThrow(lastException);
    try {
        client.beginTransaction(req).get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().hasCauseThat().isEqualTo(lastException);
        if (asyncStackTraceCaptureEnabled) {
            assertThat(e).hasMessageThat().contains("stack trace when async call was initiated");
        } else {
            assertThat(e).hasMessageThat().contains("(stack trace capture for async call is disabled)");
        }
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException)

Example 20 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ClientImplTest method testRetries_NonRetryable.

@Test
public void testRetries_NonRetryable() throws Exception {
    BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
    CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 2);
    DatastoreException lastException = nonRetryableException();
    when(datastore.beginTransaction(req)).thenThrow(lastException);
    try {
        client.beginTransaction(req).get();
        fail("expected exception");
    } catch (ExecutionException e) {
        assertThat(e).hasCauseThat().hasCauseThat().isEqualTo(lastException);
    }
}
Also used : BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) DatastoreException(com.google.datastore.v1.client.DatastoreException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

Transaction (com.google.spanner.v1.Transaction)11 Test (org.junit.Test)10 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)8 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)5 ByteString (com.google.protobuf.ByteString)5 ExecutionException (java.util.concurrent.ExecutionException)5 DatastoreException (com.google.datastore.v1.client.DatastoreException)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 Session (com.google.spanner.v1.Session)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)3 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 TransactionOptions (com.google.spanner.v1.TransactionOptions)3 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2 FirestoreGrpc (com.google.firestore.v1.FirestoreGrpc)2 RollbackRequest (com.google.firestore.v1.RollbackRequest)2 TransactionOptions (com.google.firestore.v1.TransactionOptions)2 Empty (com.google.protobuf.Empty)2 Nullable (org.springframework.lang.Nullable)2