@@ -73,6 +73,8 @@ public class SessionPoolOptions {
7373
7474 private final boolean useMultiplexedSession ;
7575
76+ private final boolean useMultiplexedSessionForRW ;
77+
7678 // TODO: Change to use java.time.Duration.
7779 private final Duration multiplexedSessionMaintenanceDuration ;
7880
@@ -108,6 +110,13 @@ private SessionPoolOptions(Builder builder) {
108110 (useMultiplexedSessionFromEnvVariable != null )
109111 ? useMultiplexedSessionFromEnvVariable
110112 : builder .useMultiplexedSession ;
113+ // useMultiplexedSessionForRW priority => Environment var > private setter > client default
114+ Boolean useMultiplexedSessionForRWFromEnvVariable =
115+ getUseMultiplexedSessionForRWFromEnvVariable ();
116+ this .useMultiplexedSessionForRW =
117+ (useMultiplexedSessionForRWFromEnvVariable != null )
118+ ? useMultiplexedSessionForRWFromEnvVariable
119+ : builder .useMultiplexedSessionForRW ;
111120 this .multiplexedSessionMaintenanceDuration = builder .multiplexedSessionMaintenanceDuration ;
112121 }
113122
@@ -144,6 +153,7 @@ public boolean equals(Object o) {
144153 this .inactiveTransactionRemovalOptions , other .inactiveTransactionRemovalOptions )
145154 && Objects .equals (this .poolMaintainerClock , other .poolMaintainerClock )
146155 && Objects .equals (this .useMultiplexedSession , other .useMultiplexedSession )
156+ && Objects .equals (this .useMultiplexedSessionForRW , other .useMultiplexedSessionForRW )
147157 && Objects .equals (
148158 this .multiplexedSessionMaintenanceDuration ,
149159 other .multiplexedSessionMaintenanceDuration );
@@ -174,6 +184,7 @@ public int hashCode() {
174184 this .inactiveTransactionRemovalOptions ,
175185 this .poolMaintainerClock ,
176186 this .useMultiplexedSession ,
187+ this .useMultiplexedSessionForRW ,
177188 this .multiplexedSessionMaintenanceDuration );
178189 }
179190
@@ -307,6 +318,14 @@ public boolean getUseMultiplexedSession() {
307318 return useMultiplexedSession ;
308319 }
309320
321+ @ VisibleForTesting
322+ @ InternalApi
323+ public boolean getUseMultiplexedSessionForRW () {
324+ // Multiplexed sessions for R/W are enabled only if both global multiplexed sessions and
325+ // read-write multiplexed session flags are set to true.
326+ return getUseMultiplexedSession () && useMultiplexedSessionForRW ;
327+ }
328+
310329 private static Boolean getUseMultiplexedSessionFromEnvVariable () {
311330 String useMultiplexedSessionFromEnvVariable =
312331 System .getenv ("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS" );
@@ -323,6 +342,12 @@ private static Boolean getUseMultiplexedSessionFromEnvVariable() {
323342 return null ;
324343 }
325344
345+ private static Boolean getUseMultiplexedSessionForRWFromEnvVariable () {
346+ // Checks the value of env, GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW
347+ // This returns null until RW is supported.
348+ return null ;
349+ }
350+
326351 Duration getMultiplexedSessionMaintenanceDuration () {
327352 return multiplexedSessionMaintenanceDuration ;
328353 }
@@ -529,6 +554,12 @@ public static class Builder {
529554 // Set useMultiplexedSession to true to make multiplexed session the default.
530555 private boolean useMultiplexedSession = false ;
531556
557+ // This field controls the default behavior of session management for RW operations in Java
558+ // client.
559+ // Set useMultiplexedSessionForRW to true to make multiplexed session for RW operations the
560+ // default.
561+ private boolean useMultiplexedSessionForRW = false ;
562+
532563 private Duration multiplexedSessionMaintenanceDuration = Duration .ofDays (7 );
533564 private Clock poolMaintainerClock = Clock .INSTANCE ;
534565
@@ -570,6 +601,7 @@ private Builder(SessionPoolOptions options) {
570601 this .randomizePositionQPSThreshold = options .randomizePositionQPSThreshold ;
571602 this .inactiveTransactionRemovalOptions = options .inactiveTransactionRemovalOptions ;
572603 this .useMultiplexedSession = options .useMultiplexedSession ;
604+ this .useMultiplexedSessionForRW = options .useMultiplexedSessionForRW ;
573605 this .multiplexedSessionMaintenanceDuration = options .multiplexedSessionMaintenanceDuration ;
574606 this .poolMaintainerClock = options .poolMaintainerClock ;
575607 }
@@ -757,6 +789,15 @@ Builder setUseMultiplexedSession(boolean useMultiplexedSession) {
757789 return this ;
758790 }
759791
792+ /**
793+ * Sets whether the client should use multiplexed session for R/W operations or not. This method
794+ * is intentionally package-private and intended for internal use.
795+ */
796+ Builder setUseMultiplexedSessionForRW (boolean useMultiplexedSessionForRW ) {
797+ this .useMultiplexedSessionForRW = useMultiplexedSessionForRW ;
798+ return this ;
799+ }
800+
760801 @ VisibleForTesting
761802 Builder setMultiplexedSessionMaintenanceDuration (
762803 Duration multiplexedSessionMaintenanceDuration ) {
0 commit comments