Skip to content

Commit 42a5f45

Browse files
authored
transactionManager ex: tolerate aborts during read (googleapis#7188)
It is possible that txn.readRow can return an aborted error, and thus the try/catch should wrap that as well, instead of just the commit operation.
1 parent e0d28a9 commit 42a5f45

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

google-cloud-examples/src/main/java/com/google/cloud/examples/spanner/snippets/DatabaseClientSnippets.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,12 @@ public void transactionManager(final long singerId) throws InterruptedException
215215
try (TransactionManager manager = dbClient.transactionManager()) {
216216
TransactionContext txn = manager.begin();
217217
while (true) {
218-
String column = "FirstName";
219-
Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
220-
String name = row.getString(column);
221-
txn.buffer(Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
222218
try {
219+
String column = "FirstName";
220+
Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
221+
String name = row.getString(column);
222+
txn.buffer(
223+
Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
223224
manager.commit();
224225
break;
225226
} catch (AbortedException e) {

0 commit comments

Comments
 (0)