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());
}
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());
}
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());
}
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);
}
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();
}
Aggregations