109109import java .util .LinkedList ;
110110import java .util .List ;
111111import java .util .Map ;
112+ import java .util .Optional ;
112113import java .util .Set ;
113114import java .util .Stack ;
114115import java .util .UUID ;
@@ -326,28 +327,18 @@ static UnitOfWorkType of(TransactionMode transactionMode) {
326327 spanner , options .getDatabaseId (), options .getDialect ());
327328 }
328329 DatabaseClient tempDbClient = null ;
330+ final DatabaseId databaseId = options .getDatabaseId ();
329331 try {
330- final Map <String , ConnectionPropertyValue <?>> propertyValueMap = options .getInitialConnectionPropertyValues ();
331- String clientIdString = null ;
332- if (propertyValueMap != null ) {
333- final ConnectionPropertyValue <?> clientIdProp = propertyValueMap .get (CLIENT_ID );
334- if (clientIdProp != null ) {
335- Object value = clientIdProp .getValue ();
336- if (value != null ) {
337- clientIdString = value .toString ();
338- }
339- }
340- if (clientIdString != null && !clientIdString .isEmpty ()) {
341- tempDbClient = spanner .getDatabaseClient (options .getDatabaseId (), clientIdString );
342- }
343- else {
344- tempDbClient = spanner .getDatabaseClient (options .getDatabaseId ());
345- }
346- } else {
347- tempDbClient = spanner .getDatabaseClient (options .getDatabaseId ());
332+ Optional <String > clientIdOpt = extractClientIdOptional (options );
333+ if (clientIdOpt .isPresent ()) {
334+ tempDbClient = spanner .getDatabaseClient (databaseId , clientIdOpt .get ());
348335 }
349336 } catch (Exception e ) {
350- tempDbClient = spanner .getDatabaseClient (options .getDatabaseId ());
337+ System .err .println ("WARNING: Failed during DatabaseClient initialization (possibly getting specific ID), falling back to default. Error: "
338+ + e .getMessage ());
339+ }
340+ if (tempDbClient == null ) {
341+ tempDbClient = spanner .getDatabaseClient (databaseId );
351342 }
352343 this .dbClient = tempDbClient ;
353344 this .batchClient = spanner .getBatchClient (options .getDatabaseId ());
@@ -366,6 +357,14 @@ && getDialect() == Dialect.POSTGRESQL
366357 setDefaultTransactionOptions (getDefaultIsolationLevel ());
367358 }
368359
360+ private Optional <String > extractClientIdOptional (ConnectionOptions options ) {
361+ return Optional .ofNullable (options .getInitialConnectionPropertyValues ())
362+ .map (props -> props .get (CLIENT_ID ))
363+ .map (ConnectionPropertyValue ::getValue )
364+ .map (Object ::toString )
365+ .filter (id -> !id .isEmpty ());
366+ }
367+
369368 /** Constructor only for test purposes. */
370369 @ VisibleForTesting
371370 ConnectionImpl (
0 commit comments