Skip to content

Commit a2f790a

Browse files
committed
feat: support option for setting client-id
1 parent b2079f4 commit a2f790a

File tree

5 files changed

+35
-35
lines changed

5 files changed

+35
-35
lines changed

google-cloud-spanner/clirr-ignored-differences.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -964,11 +964,4 @@
964964
<className>com/google/cloud/spanner/DatabaseClient</className>
965965
<method>com.google.cloud.spanner.Statement$StatementFactory getStatementFactory()</method>
966966
</difference>
967-
968-
<difference>
969-
<differenceType>7012</differenceType>
970-
<className>com/google/cloud/spanner/Spanner</className>
971-
<method>com.google.cloud.spanner.DatabaseClient getDatabaseClient(com.google.cloud.spanner.DatabaseId, java.lang.String)</method>
972-
<justification>Added as default method for JDBC client ID support. Default implementation throws UnsupportedOperationException to maintain binary compatibility for existing implementers.</justification>
973-
</difference>
974967
</differences>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.google.cloud.spanner;
2+
3+
public interface ExtendedSpanner extends Spanner {
4+
/**
5+
* Returns a {@code DatabaseClient} for the given database and given client id. It uses a pool of
6+
* sessions to talk to the database.
7+
* <!--SNIPPET get_db_client-->
8+
*
9+
* <pre>{@code
10+
* SpannerOptions options = SpannerOptions.newBuilder().build();
11+
* Spanner spanner = options.getService();
12+
* final String project = "test-project";
13+
* final String instance = "test-instance";
14+
* final String database = "example-db";
15+
* final String client_id = "client_id"
16+
* DatabaseId db =
17+
* DatabaseId.of(project, instance, database);
18+
*
19+
* DatabaseClient dbClient = spanner.getDatabaseClient(db, client_id);
20+
* }</pre>
21+
*
22+
* <!--SNIPPET get_db_client-->
23+
*/
24+
default DatabaseClient getDatabaseClient(DatabaseId db, String clientId) {
25+
throw new UnsupportedOperationException(
26+
"getDatabaseClient with clientId is not supported by this default implementation.");
27+
}
28+
}

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

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

128-
/**
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.
131-
* <!--SNIPPET get_db_client-->
132-
*
133-
* <pre>{@code
134-
* SpannerOptions options = SpannerOptions.newBuilder().build();
135-
* Spanner spanner = options.getService();
136-
* final String project = "test-project";
137-
* final String instance = "test-instance";
138-
* final String database = "example-db";
139-
* final String client_id = "client_id"
140-
* DatabaseId db =
141-
* DatabaseId.of(project, instance, database);
142-
*
143-
* DatabaseClient dbClient = spanner.getDatabaseClient(db, client_id);
144-
* }</pre>
145-
*
146-
* <!--SNIPPET get_db_client-->
147-
*/
148-
default DatabaseClient getDatabaseClient(DatabaseId db, String clientId) {
149-
throw new UnsupportedOperationException(
150-
"getDatabaseClient with clientId is not supported by this default implementation.");
151-
}
152-
153128
/**
154129
* Returns a {@code BatchClient} to do batch operations on Cloud Spanner databases. Batch client
155130
* is useful when one wants to read/query a large amount of data from Cloud Spanner across

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import javax.annotation.concurrent.GuardedBy;
6060

6161
/** Default implementation of the Cloud Spanner interface. */
62-
class SpannerImpl extends BaseService<SpannerOptions> implements Spanner {
62+
class SpannerImpl extends BaseService<SpannerOptions> implements ExtendedSpanner {
6363
private static final Logger logger = Logger.getLogger(SpannerImpl.class.getName());
6464
final TraceWrapper tracer =
6565
new TraceWrapper(

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.google.cloud.spanner.DatabaseId;
5959
import com.google.cloud.spanner.Dialect;
6060
import com.google.cloud.spanner.ErrorCode;
61+
import com.google.cloud.spanner.ExtendedSpanner;
6162
import com.google.cloud.spanner.Mutation;
6263
import com.google.cloud.spanner.Options;
6364
import com.google.cloud.spanner.Options.QueryOption;
@@ -328,8 +329,11 @@ static UnitOfWorkType of(TransactionMode transactionMode) {
328329
final DatabaseId databaseId = options.getDatabaseId();
329330
try {
330331
Optional<String> clientIdOpt = extractClientIdOptional(options);
331-
if (clientIdOpt.isPresent()) {
332-
tempDbClient = spanner.getDatabaseClient(databaseId, clientIdOpt.get());
332+
if (clientIdOpt.isPresent() && !clientIdOpt.get().isEmpty()) {
333+
if (this.spanner instanceof ExtendedSpanner) {
334+
ExtendedSpanner extendedSpanner = (ExtendedSpanner) this.spanner;
335+
tempDbClient = extendedSpanner.getDatabaseClient(databaseId, clientIdOpt.get());
336+
}
333337
}
334338
} catch (Exception e) {
335339
System.err.println(

0 commit comments

Comments
 (0)