Skip to content

Commit b076102

Browse files
chore: Refactor SI options in CloudClientExecutor (#13697)
chore: Refactor SI options handling to include read lock mode and isolation level based on conditions. --------- Co-authored-by: Sakthivel Subramanian <179120858+sakthivelmanii@users.noreply.github.com>
1 parent f896639 commit b076102

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

java-spanner/google-cloud-spanner-executor/src/main/java/com/google/cloud/executor/spanner/CloudClientExecutor.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,14 @@ public void startRWTransaction() throws Exception {
394394
List<TransactionOption> transactionOptions = new ArrayList<>();
395395
if (repeatableRead) {
396396
transactionOptions.add(Options.isolationLevel(IsolationLevel.REPEATABLE_READ));
397-
} else {
398-
transactionOptions.add(Options.isolationLevel(IsolationLevel.SERIALIZABLE));
397+
transactionOptions.add(
398+
Options.readLockMode(
399+
optimistic ? ReadLockMode.OPTIMISTIC : ReadLockMode.PESSIMISTIC));
399400
}
400401
if (!repeatableRead && optimistic) {
402+
transactionOptions.add(Options.isolationLevel(IsolationLevel.SERIALIZABLE));
401403
transactionOptions.add(Options.readLockMode(ReadLockMode.OPTIMISTIC));
402404
}
403-
if (repeatableRead && !optimistic) {
404-
transactionOptions.add(Options.readLockMode(ReadLockMode.PESSIMISTIC));
405-
}
406405
runner =
407406
dbClient.readWriteTransaction(
408407
transactionOptions.toArray(
@@ -786,11 +785,13 @@ public synchronized Status finish(Mode finishMode, OutcomeSender sender) {
786785
if (rwTxn.getTimestamp() != null) {
787786
outcomeBuilder.setCommitTime(rwTxn.getTimestamp());
788787
}
789-
if (finishMode == Mode.COMMIT
790-
&& rwTxn.runner.getCommitResponse().getSnapshotTimestamp() != null) {
791-
outcomeBuilder.setSnapshotIsolationTxnReadTimestamp(
792-
Timestamps.toMicros(
793-
rwTxn.runner.getCommitResponse().getSnapshotTimestamp().toProto()));
788+
if (finishMode == Mode.COMMIT && rwTxn.runner.getCommitResponse() != null) {
789+
com.google.cloud.spanner.CommitResponse commitResponse =
790+
rwTxn.runner.getCommitResponse();
791+
if (commitResponse.getSnapshotTimestamp() != null) {
792+
outcomeBuilder.setSnapshotIsolationTxnReadTimestamp(
793+
Timestamps.toMicros(commitResponse.getSnapshotTimestamp().toProto()));
794+
}
794795
}
795796
clear();
796797
}

0 commit comments

Comments
 (0)