diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunnerImpl.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunnerImpl.java index 9e9fe62304..b3d37a37d4 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunnerImpl.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunnerImpl.java @@ -222,6 +222,9 @@ public void removeListener(Runnable listener) { private final Map channelHint; + // This field indicates whether the read-write transaction contains only mutation operations. + boolean mutationsOnlyTransaction = false; + private TransactionContextImpl(Builder builder) { super(builder); this.transactionId = builder.transactionId; @@ -402,6 +405,11 @@ ApiFuture commitAsync() { synchronized (lock) { if (transactionIdFuture == null && transactionId == null && runningAsyncOperations == 0) { finishOps = SettableApiFuture.create(); + // At this point, it is ensured that the transaction contains only mutations. Adding a + // safeguard to apply this only for multiplexed sessions. + if (session.getIsMultiplexed()) { + mutationsOnlyTransaction = true; + } createTxnAsync(finishOps, randomMutation); } else { finishOps = finishedAsyncOperations; @@ -1229,7 +1237,7 @@ private T runInternal(final TransactionCallable txCallable) { if (attempt.get() > 0) { // Do not inline the BeginTransaction during a retry if the initial attempt did not // actually start a transaction. - useInlinedBegin = txn.transactionId != null; + useInlinedBegin = txn.mutationsOnlyTransaction || txn.transactionId != null; // Determine the latest transactionId when using a multiplexed session. ByteString multiplexedSessionPreviousTransactionId = ByteString.EMPTY;