Search in sources :

Example 11 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testErrorHandlingInImplicitTransaction.

@Test
public void testErrorHandlingInImplicitTransaction() throws SQLException {
    String sql = String.format("%s; %s; %s; %s; %s;", INSERT_STATEMENT, "COMMIT", SELECT1, INVALID_SELECT, SELECT2);
    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 transaction was rolled back and that the connection is usable.
            assertTrue(statement.execute("show transaction isolation level"));
        }
    }
    List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
    assertEquals(3, requests.size());
    assertEquals(INSERT_STATEMENT.getSql(), requests.get(0).getSql());
    assertTrue(requests.get(0).getTransaction().hasBegin());
    // The rest of the statements in the batch are all selects, so PGAdapter tries to use a
    // read-only transaction.
    assertEquals(1, mockSpanner.getRequestsOfType(BeginTransactionRequest.class).size());
    assertTrue(mockSpanner.getRequestsOfType(BeginTransactionRequest.class).get(0).getOptions().hasReadOnly());
    assertEquals(SELECT1.getSql(), requests.get(1).getSql());
    assertTrue(requests.get(1).getTransaction().hasId());
    assertEquals(INVALID_SELECT.getSql(), requests.get(2).getSql());
    assertTrue(requests.get(2).getTransaction().hasId());
    // We get one commit for the read/write transaction. The read-only transaction is not committed
    // or rolled back, as that is not necessary for read-only transactions.
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
    List<RollbackRequest> rollbackRequests = mockSpanner.getRequestsOfType(RollbackRequest.class);
    assertEquals(0, rollbackRequests.size());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Statement(java.sql.Statement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RollbackRequest(com.google.spanner.v1.RollbackRequest) Test(org.junit.Test)

Example 12 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testErrorHandlingInTwoDmlStatements.

@Test
public void testErrorHandlingInTwoDmlStatements() throws SQLException {
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (java.sql.Statement statement = connection.createStatement()) {
            SQLException exception = assertThrows(SQLException.class, () -> statement.execute(String.format("%s; %s;", INSERT_STATEMENT, INVALID_DML)));
            assertTrue(exception.getMessage().contains("INVALID_ARGUMENT: Statement is invalid."));
        }
    }
    // Verify that the DML statements were batched together by PgAdapter.
    List<ExecuteBatchDmlRequest> requests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
    assertEquals(1, requests.size());
    ExecuteBatchDmlRequest request = requests.get(0);
    assertEquals(2, request.getStatementsCount());
    assertEquals(INSERT_STATEMENT.getSql(), request.getStatements(0).getSql());
    assertEquals(INVALID_DML.getSql(), request.getStatements(1).getSql());
    // Verify that the implicit transaction is rolled back due to the exception
    List<RollbackRequest> rollbackRequests = mockSpanner.getRequestsOfType(RollbackRequest.class);
    assertEquals(1, rollbackRequests.size());
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Statement(java.sql.Statement) RollbackRequest(com.google.spanner.v1.RollbackRequest) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Example 13 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testErrorHandlingInExplicitTransactionWithCommit.

@Test
public void testErrorHandlingInExplicitTransactionWithCommit() throws SQLException {
    String sql = String.format("%s; %s; %s; %s; %s;", INSERT_STATEMENT, "BEGIN", UPDATE_STATEMENT, INVALID_DML, "COMMIT");
    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."));
            // Execute a client side statement to verify that the transaction is in the aborted state.
            exception = assertThrows(SQLException.class, () -> statement.execute("show transaction isolation level"));
            assertTrue(exception.getMessage(), exception.getMessage().contains(TRANSACTION_ABORTED_ERROR));
            // Rollback the transaction and verify that we can get out of the aborted state.
            statement.execute("rollback work");
            assertTrue(statement.execute("show transaction isolation level"));
        }
    }
    // The first DML statement is executed separately, as it is followed by a non-DML statement.
    // The BEGIN statement will switch the batch to use an explicit transaction. The first DML
    // statement will be included as part of that explicit transaction.
    List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
    assertEquals(1, requests.size());
    assertEquals(INSERT_STATEMENT.getSql(), requests.get(0).getSql());
    // Verify that the DML statements were batched together by PgAdapter.
    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 explicit transaction is rolled back by PGAdapter. The
    List<RollbackRequest> rollbackRequests = mockSpanner.getRequestsOfType(RollbackRequest.class);
    assertEquals(1, rollbackRequests.size());
    // The transaction should not be committed as it fails on the invalid DML statement.
    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)

Example 14 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest 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 15 with RollbackRequest

use of com.google.firestore.v1beta1.RollbackRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class Rollback method rollbackCall.

public void rollbackCall() {
    System.out.println("\n:: Rolling Back Transaction ::\n");
    if (Main.transactionId == null) {
        System.out.println("WARNING:  No current transaction open, run BeginTransaction first...");
        return;
    } else {
        System.out.println("Found Transaction ID '" + Main.transactionId.toString() + "'.  Committing....");
    }
    FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    RollbackRequest rollbackRequest = RollbackRequest.newBuilder().setTransaction(Main.transactionId).setDatabase("projects/firestoretestclient/databases/(default)").build();
    try {
        blockingStub.rollback(rollbackRequest);
    } catch (Exception e) {
        System.out.println("Error during call: " + e.getMessage() + e.getCause());
        return;
    }
    System.out.println("Success!");
    Menu menu = new Menu();
    menu.draw();
}
Also used : GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) Menu(org.roguewave.grpc.util.gfx.Menu) RollbackRequest(com.google.firestore.v1beta1.RollbackRequest)

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