Skip to content

Commit 27c1d95

Browse files
committed
feat: support option for setting client-id
1 parent 3c587df commit 27c1d95

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,9 @@ public interface Spanner extends Service<SpannerOptions>, AutoCloseable {
125125
*/
126126
DatabaseClient getDatabaseClient(DatabaseId db);
127127

128-
129128
/**
130-
* Returns a {@code DatabaseClient} for the given database and given client id. It uses a pool of sessions to talk to
131-
* the database.
129+
* Returns a {@code DatabaseClient} for the given database and given client id. It uses a pool of
130+
* sessions to talk to the database.
132131
* <!--SNIPPET get_db_client-->
133132
*
134133
* <pre>{@code

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionImpl.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
import java.util.Iterator;
109109
import java.util.LinkedList;
110110
import java.util.List;
111-
import java.util.Map;
112111
import java.util.Optional;
113112
import java.util.Set;
114113
import java.util.Stack;
@@ -254,8 +253,7 @@ static UnitOfWorkType of(TransactionMode transactionMode) {
254253
}
255254
}
256255

257-
private StatementTimeout statementTimeout =
258-
new StatementTimeout();
256+
private StatementTimeout statementTimeout = new StatementTimeout();
259257
private boolean closed = false;
260258

261259
private final Spanner spanner;
@@ -330,14 +328,15 @@ static UnitOfWorkType of(TransactionMode transactionMode) {
330328
final DatabaseId databaseId = options.getDatabaseId();
331329
try {
332330
Optional<String> clientIdOpt = extractClientIdOptional(options);
333-
if(clientIdOpt.isPresent()) {
331+
if (clientIdOpt.isPresent()) {
334332
tempDbClient = spanner.getDatabaseClient(databaseId, clientIdOpt.get());
335333
}
336-
} catch(Exception e) {
337-
System.err.println("WARNING: Failed during DatabaseClient initialization (possibly getting specific ID), falling back to default. Error: "
338-
+ e.getMessage());
334+
} catch (Exception e) {
335+
System.err.println(
336+
"WARNING: Failed during DatabaseClient initialization (possibly getting specific ID), falling back to default. Error: "
337+
+ e.getMessage());
339338
}
340-
if(tempDbClient == null) {
339+
if (tempDbClient == null) {
341340
tempDbClient = spanner.getDatabaseClient(databaseId);
342341
}
343342
this.dbClient = tempDbClient;
@@ -566,8 +565,7 @@ public boolean isClosed() {
566565
return closed;
567566
}
568567

569-
private <T> T getConnectionPropertyValue(
570-
ConnectionProperty<T> property) {
568+
private <T> T getConnectionPropertyValue(ConnectionProperty<T> property) {
571569
return this.connectionState.getValue(property).getValue();
572570
}
573571

@@ -587,9 +585,8 @@ private <T> void setConnectionPropertyValue(
587585
/**
588586
* Sets a connection property value only for the duration of the current transaction. The effects
589587
* of this will be undone once the transaction ends, regardless whether the transaction is
590-
* committed or rolled back. 'Local' properties are supported for both {@link
591-
* Type#TRANSACTIONAL} and {@link
592-
* Type#NON_TRANSACTIONAL} connection states.
588+
* committed or rolled back. 'Local' properties are supported for both {@link Type#TRANSACTIONAL}
589+
* and {@link Type#NON_TRANSACTIONAL} connection states.
593590
*
594591
* <p>NOTE: This feature is not yet exposed in the public API.
595592
*/

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.cloud.spanner.connection.ConnectionProperties.AUTO_PARTITION_MODE;
2222
import static com.google.cloud.spanner.connection.ConnectionProperties.CHANNEL_PROVIDER;
2323
import static com.google.cloud.spanner.connection.ConnectionProperties.CLIENT_CERTIFICATE;
24+
import static com.google.cloud.spanner.connection.ConnectionProperties.CLIENT_ID;
2425
import static com.google.cloud.spanner.connection.ConnectionProperties.CLIENT_KEY;
2526
import static com.google.cloud.spanner.connection.ConnectionProperties.CREDENTIALS_PROVIDER;
2627
import static com.google.cloud.spanner.connection.ConnectionProperties.CREDENTIALS_URL;
@@ -46,7 +47,6 @@
4647
import static com.google.cloud.spanner.connection.ConnectionProperties.RETURN_COMMIT_STATS;
4748
import static com.google.cloud.spanner.connection.ConnectionProperties.ROUTE_TO_LEADER;
4849
import static com.google.cloud.spanner.connection.ConnectionProperties.TRACING_PREFIX;
49-
import static com.google.cloud.spanner.connection.ConnectionProperties.CLIENT_ID;
5050
import static com.google.cloud.spanner.connection.ConnectionProperties.TRACK_CONNECTION_LEAKS;
5151
import static com.google.cloud.spanner.connection.ConnectionProperties.TRACK_SESSION_LEAKS;
5252
import static com.google.cloud.spanner.connection.ConnectionProperties.USER_AGENT;

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,18 +370,21 @@ public void testGetDatabaseClient_when_clientId_is_not_null() {
370370
.thenReturn(SessionPoolOptions.newBuilder().setMinSessions(0).build());
371371
Mockito.when(spannerOptions.getDatabaseRole()).thenReturn("role");
372372

373-
DatabaseClientImpl databaseClient = (DatabaseClientImpl) impl.getDatabaseClient(db, "clientId-1");
373+
DatabaseClientImpl databaseClient =
374+
(DatabaseClientImpl) impl.getDatabaseClient(db, "clientId-1");
374375
assertThat(databaseClient.clientId).isEqualTo("clientId-1");
375376

376377
// Get same db client again.
377-
DatabaseClientImpl databaseClient1 = (DatabaseClientImpl) impl.getDatabaseClient(db, "clientId-1");
378+
DatabaseClientImpl databaseClient1 =
379+
(DatabaseClientImpl) impl.getDatabaseClient(db, "clientId-1");
378380
assertThat(databaseClient1.clientId).isEqualTo(databaseClient.clientId);
379381

380382
// Get a db client for a different database.
381383
String dbName2 =
382384
String.format("projects/p1/instances/i1/databases/%s", UUID.randomUUID().toString());
383385
DatabaseId db2 = DatabaseId.of(dbName2);
384-
DatabaseClientImpl databaseClient2 = (DatabaseClientImpl) impl.getDatabaseClient(db2, "clientId-1");
386+
DatabaseClientImpl databaseClient2 =
387+
(DatabaseClientImpl) impl.getDatabaseClient(db2, "clientId-1");
385388
assertThat(databaseClient2.clientId).isEqualTo("clientId-1");
386389

387390
// Getting a new database client for an invalidated database should use the same client id.

0 commit comments

Comments
 (0)