Skip to content

Commit

Permalink
fix: clear statement tag before auto rollback (#147)
Browse files Browse the repository at this point in the history
Clear any statement tag before executing an automatic rollback when a
transaction is aborted, as statement tags are not allowed for rollback
requests.

Fixes #146
  • Loading branch information
olavloite authored May 5, 2022
1 parent 23702ee commit 994976f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public void handleQuery() throws Exception {
if (connection.getStatus() == ConnectionStatus.TRANSACTION_ABORTED) {
transactionStatus = Status.FAILED;
// Actively rollback the aborted transaction but still block clients
// Clear any statement tags, as these are not allowed for rollbacks.
connection.getSpannerConnection().setStatementTag(null);
connection.getSpannerConnection().rollback();
} else {
transactionStatus = Status.TRANSACTION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,4 +842,20 @@ public void testAbortedTransactionRollbackInAutocommit() throws SQLException {
List<RollbackRequest> rollbackRequests = mockSpanner.getRequestsOfType(RollbackRequest.class);
assertEquals(1, rollbackRequests.size());
}

@Test
public void testStatementTagError() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
assertFalse(statement.execute("begin"));
assertFalse(statement.execute(INSERT_STATEMENT.getSql()));
assertFalse(statement.execute("set spanner.statement_tag='foo'"));
// Execute an invalid statement.
assertThrows(SQLException.class, () -> statement.execute("set statement_timeout=2s"));
// Make sure that we actually received a Rollback statement. The rollback was initiated by
// PGAdapter when the transaction was aborted.
assertEquals(1, mockSpanner.countRequestsOfType(RollbackRequest.class));
}
}
}
}

0 comments on commit 994976f

Please sign in to comment.