Skip to content

Commit

Permalink
test: constraint error causes transaction to be invalidated
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Jul 30, 2020
1 parent a4d2e76 commit 1817afa
Showing 1 changed file with 39 additions and 0 deletions.
Expand Up @@ -578,6 +578,45 @@ public Long run(TransactionContext transaction) throws Exception {
assertThat(updateCount).isEqualTo(1L);
}

@Test
public void testTxWithConstraintError() {
assumeFalse(
"Emulator does not recover from an error within a transaction",
env.getTestHelper().isEmulator());

// First insert a single row.
client.writeAtLeastOnce(
ImmutableList.of(
Mutation.newInsertOrUpdateBuilder("T").set("K").to("One").set("V").to(1L).build()));

long updateCount =
client
.readWriteTransaction()
.run(
new TransactionCallable<Long>() {
@Override
public Long run(TransactionContext transaction) throws Exception {
try {
// Try to insert a duplicate row. This statement will fail. When the statement
// is executed against an already existing transaction (i.e.
// inlineBegin=false), the entire transaction will remain invalid and cannot
// be committed. When it is executed as the first statement of a transaction
// that also tries to start a transaction, then no transaction will be started
// and the next statement will start the transaction. This will cause the
// transaction to succeed.
transaction.executeUpdate(
Statement.of("INSERT INTO T (K, V) VALUES ('One', 1)"));
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.ALREADY_EXISTS);
}
return transaction.executeUpdate(
Statement.of("INSERT INTO T (K, V) VALUES ('Two', 2)"));
}
});
assertThat(updateCount).isEqualTo(1L);
}

@Test
public void testTxWithUncaughtError() {
try {
Expand Down

0 comments on commit 1817afa

Please sign in to comment.