@@ -284,6 +284,9 @@ public void close() {
284284 /** The current multiplexed session that is used by this client. */
285285 private final AtomicReference <ApiFuture <SessionReference >> multiplexedSessionReference ;
286286
287+ /** The in progress multiplexed session reference that is used by this client. */
288+ private final AtomicReference <ApiFuture <SessionReference >> inProgressMultiplexedSessionReference ;
289+
287290 /**
288291 * The Transaction response returned by the BeginTransaction request with read-write when a
289292 * multiplexed session is created during client initialization.
@@ -320,7 +323,7 @@ public void close() {
320323 * This flag is set to true if create session RPC is in progress. This flag prevents application
321324 * from firing two requests concurrently
322325 */
323- private final AtomicBoolean retryingSessionCreation = new AtomicBoolean (false );
326+ private final AtomicBoolean sessionCreationInProgress = new AtomicBoolean (true );
324327
325328 /**
326329 * This lock is used to prevent two threads from retrying createSession RPC requests in
@@ -371,6 +374,7 @@ public void close() {
371374 SettableApiFuture .create ();
372375 this .readWriteBeginTransactionReferenceFuture = SettableApiFuture .create ();
373376 this .multiplexedSessionReference = new AtomicReference <>(initialSessionReferenceFuture );
377+ this .inProgressMultiplexedSessionReference = new AtomicReference <>(initialSessionReferenceFuture );
374378 asyncCreateMultiplexedSession (initialSessionReferenceFuture );
375379 maybeWaitForSessionCreation (
376380 sessionClient .getSpanner ().getOptions ().getSessionPoolOptions (),
@@ -383,7 +387,8 @@ private void asyncCreateMultiplexedSession(
383387 new SessionConsumer () {
384388 @ Override
385389 public void onSessionReady (SessionImpl session ) {
386- retryingSessionCreation .set (false );
390+ sessionCreationInProgress .set (false );
391+ multiplexedSessionReference .set (sessionReferenceFuture );
387392 sessionReferenceFuture .set (session .getSessionReference ());
388393 // only start the maintainer if we actually managed to create a session in the first
389394 // place.
@@ -416,7 +421,8 @@ public void onSessionReady(SessionImpl session) {
416421 public void onSessionCreateFailure (Throwable t , int createFailureForSessionCount ) {
417422 // Mark multiplexes sessions as unimplemented and fall back to regular sessions if
418423 // UNIMPLEMENTED is returned.
419- retryingSessionCreation .set (false );
424+ sessionCreationInProgress .set (false );
425+ multiplexedSessionReference .set (sessionReferenceFuture );
420426 maybeMarkUnimplemented (t );
421427 sessionReferenceFuture .setException (t );
422428 }
@@ -567,13 +573,12 @@ MultiplexedSessionMaintainer getMaintainer() {
567573 }
568574
569575 ApiFuture <SessionReference > getCurrentSessionReferenceFuture () {
570- System .out .println ("Accessing Multiplexed Session " + Instant .now ());
571576 return ApiFutures .catchingAsync (
572577 this .multiplexedSessionReference .get (),
573578 Throwable .class ,
574579 (throwable ) -> {
575580 maybeRetrySessionCreation ();
576- return this .multiplexedSessionReference .get ();
581+ return this .inProgressMultiplexedSessionReference .get ();
577582 },
578583 MoreExecutors .directExecutor ());
579584 }
@@ -583,11 +588,10 @@ private void maybeRetrySessionCreation() {
583588 try {
584589 if (isValid ()
585590 && isMultiplexedSessionsSupported ()
586- && retryingSessionCreation .compareAndSet (false , true )) {
587- System .out .println ("Retrying Multiplexed Session " + Instant .now ());
591+ && sessionCreationInProgress .compareAndSet (false , true )) {
588592 SettableApiFuture <SessionReference > settableApiFuture = SettableApiFuture .create ();
589593 asyncCreateMultiplexedSession (settableApiFuture );
590- multiplexedSessionReference .set (settableApiFuture );
594+ inProgressMultiplexedSessionReference .set (settableApiFuture );
591595 }
592596 } finally {
593597 sessionCreationLock .unlock ();
@@ -840,6 +844,7 @@ void maintain() {
840844 new SessionConsumer () {
841845 @ Override
842846 public void onSessionReady (SessionImpl session ) {
847+ System .out .println ("Session ready" );
843848 multiplexedSessionReference .set (
844849 ApiFutures .immediateFuture (session .getSessionReference ()));
845850 expirationDate .set (
0 commit comments