Search in sources :

Example 16 with BeginTransactionRequest

use of com.google.datastore.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 17 with BeginTransactionRequest

use of com.google.datastore.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 18 with BeginTransactionRequest

use of com.google.datastore.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 19 with BeginTransactionRequest

use of com.google.datastore.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)

Example 20 with BeginTransactionRequest

use of com.google.datastore.v1.BeginTransactionRequest in project liquibase-spanner by cloudspannerecosystem.

the class MergeColumnsTest method testMergeSingersFirstNamdAndLastNameFromYaml.

@Test
void testMergeSingersFirstNamdAndLastNameFromYaml() throws Exception {
    String[] expectedSql = new String[] { "ALTER TABLE Singers ADD COLUMN FullName STRING(500)", "ALTER TABLE Singers DROP COLUMN FirstName", "ALTER TABLE Singers DROP COLUMN LastName" };
    for (String sql : expectedSql) {
        addUpdateDdlStatementsResponse(sql);
    }
    for (String file : new String[] { "merge-singers-firstname-and-lastname.spanner.yaml" }) {
        try (Connection con = createConnection();
            Liquibase liquibase = getLiquibase(con, file)) {
            liquibase.update(new Contexts("test"));
        }
    }
    assertThat(mockAdmin.getRequests()).hasSize(expectedSql.length);
    for (int i = 0; i < expectedSql.length; i++) {
        assertThat(mockAdmin.getRequests().get(i)).isInstanceOf(UpdateDatabaseDdlRequest.class);
        UpdateDatabaseDdlRequest request = (UpdateDatabaseDdlRequest) mockAdmin.getRequests().get(i);
        assertThat(request.getStatementsList()).hasSize(1);
        assertThat(request.getStatementsList().get(0)).isEqualTo(expectedSql[i]);
    }
    // Verify that the mock server received one BeginTransactionRequest for a PDML transaction.
    assertThat(mockSpanner.getRequests().stream().filter(new Predicate<AbstractMessage>() {

        @Override
        public boolean test(AbstractMessage t) {
            return t instanceof BeginTransactionRequest;
        }
    }).map(new Function<AbstractMessage, BeginTransactionRequest>() {

        @Override
        public BeginTransactionRequest apply(AbstractMessage t) {
            return (BeginTransactionRequest) t;
        }
    }).filter(new Predicate<BeginTransactionRequest>() {

        @Override
        public boolean test(BeginTransactionRequest t) {
            return t.hasOptions() && t.getOptions().hasPartitionedDml();
        }
    }).count()).isEqualTo(1L);
}
Also used : Liquibase(liquibase.Liquibase) AbstractMessage(com.google.protobuf.AbstractMessage) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Connection(java.sql.Connection) Contexts(liquibase.Contexts) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Predicate(java.util.function.Predicate) Test(org.junit.jupiter.api.Test)

Aggregations

BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)11 Test (org.junit.Test)10 Transaction (com.google.spanner.v1.Transaction)7 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 Connection (java.sql.Connection)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)3 UpdateDatabaseDdlRequest (com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest)3 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)3 Session (com.google.spanner.v1.Session)3 TransactionOptions (com.google.spanner.v1.TransactionOptions)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)2 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2