2929import io .opentelemetry .api .common .Attributes ;
3030import java .util .ArrayList ;
3131import java .util .Arrays ;
32- import java .util .Objects ;
32+ import java .util .HashMap ;
33+ import java .util .Map ;
3334import java .util .concurrent .ExecutionException ;
3435import java .util .concurrent .Future ;
3536import java .util .concurrent .TimeUnit ;
@@ -51,6 +52,7 @@ class DatabaseClientImpl implements DatabaseClient {
5152 @ VisibleForTesting final boolean useMultiplexedSessionForRW ;
5253 private final int dbId ;
5354 private final AtomicInteger nthRequest ;
55+ private final Map <String , Integer > clientIdToOrdinalMap ;
5456
5557 final boolean useMultiplexedSessionBlindWrite ;
5658
@@ -98,18 +100,19 @@ class DatabaseClientImpl implements DatabaseClient {
98100 this .useMultiplexedSessionForRW = useMultiplexedSessionForRW ;
99101 this .commonAttributes = commonAttributes ;
100102
103+ this .clientIdToOrdinalMap = new HashMap <String , Integer >();
101104 this .dbId = this .dbIdFromClientId (this .clientId );
102105 this .nthRequest = new AtomicInteger (0 );
103106 }
104107
105108 @ VisibleForTesting
106- int dbIdFromClientId (String clientId ) {
107- int i = clientId . indexOf ( "-" );
108- String strWithValue = clientId . substring ( i + 1 );
109- if ( Objects . equals ( strWithValue , "" )) {
110- strWithValue = "0" ;
109+ synchronized int dbIdFromClientId (String clientId ) {
110+ Integer id = this . clientIdToOrdinalMap . get ( clientId );
111+ if ( id == null ) {
112+ id = this . clientIdToOrdinalMap . size () + 1 ;
113+ this . clientIdToOrdinalMap . put ( clientId , id ) ;
111114 }
112- return Integer . parseInt ( strWithValue ) ;
115+ return id ;
113116 }
114117
115118 @ VisibleForTesting
@@ -423,9 +426,13 @@ private UpdateOption[] withReqId(
423426 if (reqId == null ) {
424427 return options ;
425428 }
426- ArrayList <UpdateOption > allOptions = new ArrayList (Arrays .asList (options ));
427- allOptions .add (new Options .RequestIdOption (reqId ));
428- return allOptions .toArray (new UpdateOption [0 ]);
429+ if (options == null || options .length == 0 ) {
430+ return new UpdateOption [] {new Options .RequestIdOption (reqId )};
431+ }
432+ UpdateOption [] allOptions = new UpdateOption [options .length + 1 ];
433+ System .arraycopy (options , 0 , allOptions , 0 , options .length );
434+ allOptions [options .length ] = new Options .RequestIdOption (reqId );
435+ return allOptions ;
429436 }
430437
431438 private TransactionOption [] withReqId (
0 commit comments