Search in sources :

Example 6 with RollbackRequest

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

the class FirestoreClient method rollback.

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
 * Rolls back a transaction.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (FirestoreClient firestoreClient = FirestoreClient.create()) {
 *   String database = "database1789464955";
 *   ByteString transaction = ByteString.EMPTY;
 *   firestoreClient.rollback(database, transaction);
 * }
 * }</pre>
 *
 * @param database Required. The database name. In the format:
 *     `projects/{project_id}/databases/{database_id}`.
 * @param transaction Required. The transaction to roll back.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
public final void rollback(String database, ByteString transaction) {
    RollbackRequest request = RollbackRequest.newBuilder().setDatabase(database).setTransaction(transaction).build();
    rollback(request);
}
Also used : RollbackRequest(com.google.firestore.v1.RollbackRequest)

Example 7 with RollbackRequest

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

the class FirestoreClientTest method rollbackTest.

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

Example 8 with RollbackRequest

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

the class ReactiveFirestoreTransactionManagerTest method getFirestoreTemplate.

private FirestoreTemplate getFirestoreTemplate() {
    doAnswer(invocation -> {
        StreamObserver<BeginTransactionResponse> streamObserver = invocation.getArgument(1);
        streamObserver.onNext(BeginTransactionResponse.newBuilder().setTransaction(ByteString.copyFromUtf8("transaction1")).build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).beginTransaction(any(), any());
    doAnswer(invocation -> {
        CommitRequest commitRequest = invocation.getArgument(0);
        StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
        assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onNext(CommitResponse.newBuilder().build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).commit(any(), any());
    doAnswer(invocation -> {
        RollbackRequest rollbackRequest = invocation.getArgument(0);
        StreamObserver<Empty> streamObserver = invocation.getArgument(1);
        assertThat(rollbackRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).rollback(any(), any());
    doAnswer(invocation -> {
        GetDocumentRequest request = invocation.getArgument(0);
        StreamObserver<Document> streamObserver = invocation.getArgument(1);
        assertThat(request.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        String name = request.getName();
        streamObserver.onNext(FirestoreTemplateTests.buildDocument(name.substring(name.length() - 2), 100L));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(any(), any());
    FirestoreTemplate template = new FirestoreTemplate(this.firestoreStub, this.parent, new FirestoreDefaultClassMapper(), new FirestoreMappingContext());
    StepVerifier.setDefaultTimeout(Duration.ofSeconds(5));
    return template;
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) CommitResponse(com.google.firestore.v1.CommitResponse) ByteString(com.google.protobuf.ByteString) RollbackRequest(com.google.firestore.v1.RollbackRequest) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Document(com.google.firestore.v1.Document) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) FirestoreMappingContext(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext) Empty(com.google.protobuf.Empty) FirestoreDefaultClassMapper(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreDefaultClassMapper)

Example 9 with RollbackRequest

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

the class ReactiveFirestoreTransactionManagerTest method getFirestoreTemplate.

private FirestoreTemplate getFirestoreTemplate() {
    doAnswer(invocation -> {
        StreamObserver<BeginTransactionResponse> streamObserver = invocation.getArgument(1);
        streamObserver.onNext(BeginTransactionResponse.newBuilder().setTransaction(ByteString.copyFromUtf8("transaction1")).build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).beginTransaction(any(), any());
    doAnswer(invocation -> {
        CommitRequest commitRequest = invocation.getArgument(0);
        StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
        assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onNext(CommitResponse.newBuilder().build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).commit(any(), any());
    doAnswer(invocation -> {
        RollbackRequest rollbackRequest = invocation.getArgument(0);
        StreamObserver<Empty> streamObserver = invocation.getArgument(1);
        assertThat(rollbackRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).rollback(any(), any());
    doAnswer(invocation -> {
        GetDocumentRequest request = invocation.getArgument(0);
        StreamObserver<Document> streamObserver = invocation.getArgument(1);
        assertThat(request.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        String name = request.getName();
        streamObserver.onNext(FirestoreTemplateTests.buildDocument(name.substring(name.length() - 2), 100L));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(any(), any());
    FirestoreMappingContext mappingContext = new FirestoreMappingContext();
    FirestoreTemplate template = new FirestoreTemplate(this.firestoreStub, this.parent, new FirestoreDefaultClassMapper(mappingContext), mappingContext);
    StepVerifier.setDefaultTimeout(Duration.ofSeconds(5));
    return template;
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) CommitResponse(com.google.firestore.v1.CommitResponse) ByteString(com.google.protobuf.ByteString) RollbackRequest(com.google.firestore.v1.RollbackRequest) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Document(com.google.firestore.v1.Document) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) FirestoreMappingContext(com.google.cloud.spring.data.firestore.mapping.FirestoreMappingContext) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) Empty(com.google.protobuf.Empty) FirestoreDefaultClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreDefaultClassMapper)

Example 10 with RollbackRequest

use of com.google.spanner.v1.RollbackRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testErrorHandlingInExplicitTransactionWithoutCommit.

@Test
public void testErrorHandlingInExplicitTransactionWithoutCommit() throws SQLException {
    String sql = String.format("%s; %s; %s; %s;", INSERT_STATEMENT, "BEGIN", UPDATE_STATEMENT, INVALID_DML);
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (java.sql.Statement statement = connection.createStatement()) {
            SQLException exception = assertThrows(SQLException.class, () -> statement.execute(sql));
            assertThat(exception.getMessage(), containsString("INVALID_ARGUMENT: Statement is invalid."));
        }
    }
    // Verify that the DML statements were batched together by PgAdapter.
    List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
    assertEquals(1, requests.size());
    assertEquals(INSERT_STATEMENT.getSql(), requests.get(0).getSql());
    List<ExecuteBatchDmlRequest> batchDmlRequests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
    assertEquals(1, requests.size());
    ExecuteBatchDmlRequest request = batchDmlRequests.get(0);
    assertEquals(2, request.getStatementsCount());
    assertEquals(UPDATE_STATEMENT.getSql(), request.getStatements(0).getSql());
    assertEquals(INVALID_DML.getSql(), request.getStatements(1).getSql());
    // The aborted transaction should be rolled back by PGAdapter.
    List<RollbackRequest> rollbackRequests = mockSpanner.getRequestsOfType(RollbackRequest.class);
    assertEquals(1, rollbackRequests.size());
    // BEGIN statement converts the implicit transaction to an explicit transaction, but is
    // otherwise a no-op. We should therefore not receive any commits.
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(0, commitRequests.size());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RollbackRequest(com.google.spanner.v1.RollbackRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Statement(java.sql.Statement) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Aggregations

RollbackRequest (com.google.spanner.v1.RollbackRequest)10 Test (org.junit.Test)9 ByteString (com.google.protobuf.ByteString)5 Empty (com.google.protobuf.Empty)5 Connection (java.sql.Connection)5 SQLException (java.sql.SQLException)5 Statement (java.sql.Statement)5 RollbackRequest (com.google.firestore.v1.RollbackRequest)4 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)4 AbstractMessage (com.google.protobuf.AbstractMessage)3 CommitRequest (com.google.spanner.v1.CommitRequest)3 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)2 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2 Document (com.google.firestore.v1.Document)2 GetDocumentRequest (com.google.firestore.v1.GetDocumentRequest)2 SessionPoolTransactionContext (com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext)1 TransactionContextImpl (com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl)1