Skip to content

Commit ba76d77

Browse files
committed
chore(spanner): add comments
1 parent e56c714 commit ba76d77

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClient.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
import java.util.concurrent.atomic.AtomicLong;
5454
import java.util.concurrent.atomic.AtomicReference;
5555

56+
/**
57+
* {@link TransactionRunner} that automatically handles "UNIMPLEMENTED" errors with the message
58+
* "Transaction type read_write not supported with multiplexed sessions" by switching from a
59+
* multiplexed session to a regular session and then restarts the transaction.
60+
*/
5661
class MultiplexedSessionTransactionRunner implements TransactionRunner {
5762
private final SessionPool sessionPool;
5863
private final TransactionRunnerImpl transactionRunnerForMultiplexedSession;
@@ -91,8 +96,9 @@ public <T> T run(TransactionCallable<T> callable) {
9196
try {
9297
return getRunner().run(callable);
9398
} catch (SpannerException e) {
94-
if (e.getErrorCode() == ErrorCode.UNIMPLEMENTED) {
95-
this.isUsingMultiplexedSession = false;
99+
if (e.getErrorCode() == ErrorCode.UNIMPLEMENTED
100+
&& verifyUnimplementedErrorMessageForRWMux(e)) {
101+
this.isUsingMultiplexedSession = false; // Fallback to regular session
96102
} else {
97103
throw e; // Other errors propagate
98104
}
@@ -115,6 +121,19 @@ public TransactionRunner allowNestedTransaction() {
115121
getRunner().allowNestedTransaction();
116122
return this;
117123
}
124+
125+
private boolean verifyUnimplementedErrorMessageForRWMux(SpannerException spannerException) {
126+
if (spannerException.getCause() == null) {
127+
return false;
128+
}
129+
if (spannerException.getCause().getMessage() == null) {
130+
return false;
131+
}
132+
return spannerException
133+
.getCause()
134+
.getMessage()
135+
.contains("Transaction type read_write not supported with multiplexed sessions");
136+
}
118137
}
119138

120139
/**

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ DatabaseClientImpl createDatabaseClient(
333333
boolean useMultiplexedSessionForRW,
334334
Attributes commonAttributes) {
335335
if (multiplexedSessionClient != null) {
336+
// Set the session pool in the multiplexed session client.
337+
// This is required to handle fallback to regular sessions for in-progress transactions that
338+
// use multiplexed sessions but fail with UNIMPLEMENTED errors.
336339
multiplexedSessionClient.setPool(pool);
337340
}
338341
return new DatabaseClientImpl(

0 commit comments

Comments
 (0)