/
ConnectionOptions.java
645 lines (576 loc) · 24.9 KB
/
ConnectionOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner.jdbc;
import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Options for creating a {@link Connection} to a Google Cloud Spanner database.
*
* <p>Usage:
*
* <pre>
* <!--SNIPPET {@link ConnectionOptions} usage-->
* {@code
* ConnectionOptions options = ConnectionOptions.newBuilder()
* .setUri("cloudspanner:/projects/my_project_id/instances/my_instance_id/databases/my_database_name?autocommit=false")
* .setCredentialsUrl("/home/cloudspanner-keys/my-key.json")
* .build();
* try(Connection connection = options.getConnection()) {
* try(ResultSet rs = connection.executeQuery(Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums"))) {
* while(rs.next()) {
* // do something
* }
* }
* }
* }
* <!--SNIPPET {@link ConnectionOptions} usage-->
* </pre>
*/
class ConnectionOptions {
/** Supported connection properties that can be included in the connection URI. */
public static class ConnectionProperty {
private static final String[] BOOLEAN_VALUES = new String[] {"true", "false"};
private final String name;
private final String description;
private final String defaultValue;
private final String[] validValues;
private final int hashCode;
private static ConnectionProperty createStringProperty(String name, String description) {
return new ConnectionProperty(name, description, "", null);
}
private static ConnectionProperty createBooleanProperty(
String name, String description, boolean defaultValue) {
return new ConnectionProperty(
name, description, String.valueOf(defaultValue), BOOLEAN_VALUES);
}
private static ConnectionProperty createEmptyProperty(String name) {
return new ConnectionProperty(name, "", "", null);
}
private ConnectionProperty(
String name, String description, String defaultValue, String[] validValues) {
Preconditions.checkNotNull(name);
Preconditions.checkNotNull(description);
Preconditions.checkNotNull(defaultValue);
this.name = name;
this.description = description;
this.defaultValue = defaultValue;
this.validValues = validValues;
this.hashCode = name.toLowerCase().hashCode();
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ConnectionProperty)) {
return false;
}
return ((ConnectionProperty) o).name.equalsIgnoreCase(this.name);
}
/** @return the name of this connection property. */
public String getName() {
return name;
}
/** @return the description of this connection property. */
public String getDescription() {
return description;
}
/** @return the default value of this connection property. */
public String getDefaultValue() {
return defaultValue;
}
/**
* @return the valid values for this connection property. <code>null</code> indicates no
* restriction.
*/
public String[] getValidValues() {
return validValues;
}
}
private static final boolean DEFAULT_USE_PLAIN_TEXT = false;
static final boolean DEFAULT_AUTOCOMMIT = true;
static final boolean DEFAULT_READONLY = false;
static final boolean DEFAULT_RETRY_ABORTS_INTERNALLY = true;
private static final String DEFAULT_CREDENTIALS = null;
private static final String DEFAULT_OAUTH_TOKEN = null;
private static final String DEFAULT_NUM_CHANNELS = null;
private static final String DEFAULT_USER_AGENT = null;
private static final String PLAIN_TEXT_PROTOCOL = "http:";
private static final String HOST_PROTOCOL = "https:";
private static final String DEFAULT_HOST = "https://spanner.googleapis.com";
/** Use plain text is only for local testing purposes. */
private static final String USE_PLAIN_TEXT_PROPERTY_NAME = "usePlainText";
/** Name of the 'autocommit' connection property. */
public static final String AUTOCOMMIT_PROPERTY_NAME = "autocommit";
/** Name of the 'readonly' connection property. */
public static final String READONLY_PROPERTY_NAME = "readonly";
/** Name of the 'retry aborts internally' connection property. */
public static final String RETRY_ABORTS_INTERNALLY_PROPERTY_NAME = "retryAbortsInternally";
/** Name of the 'credentials' connection property. */
public static final String CREDENTIALS_PROPERTY_NAME = "credentials";
/**
* OAuth token to use for authentication. Cannot be used in combination with a credentials file.
*/
public static final String OAUTH_TOKEN_PROPERTY_NAME = "oauthToken";
/** Name of the 'numChannels' connection property. */
public static final String NUM_CHANNELS_PROPERTY_NAME = "numChannels";
/** Custom user agent string is only for other Google libraries. */
private static final String USER_AGENT_PROPERTY_NAME = "userAgent";
/** All valid connection properties. */
public static final Set<ConnectionProperty> VALID_PROPERTIES =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList(
ConnectionProperty.createBooleanProperty(
AUTOCOMMIT_PROPERTY_NAME, "", DEFAULT_AUTOCOMMIT),
ConnectionProperty.createBooleanProperty(
READONLY_PROPERTY_NAME, "", DEFAULT_READONLY),
ConnectionProperty.createBooleanProperty(
RETRY_ABORTS_INTERNALLY_PROPERTY_NAME, "", DEFAULT_RETRY_ABORTS_INTERNALLY),
ConnectionProperty.createStringProperty(CREDENTIALS_PROPERTY_NAME, ""),
ConnectionProperty.createStringProperty(OAUTH_TOKEN_PROPERTY_NAME, ""),
ConnectionProperty.createStringProperty(NUM_CHANNELS_PROPERTY_NAME, ""),
ConnectionProperty.createBooleanProperty(
USE_PLAIN_TEXT_PROPERTY_NAME, "", DEFAULT_USE_PLAIN_TEXT),
ConnectionProperty.createStringProperty(USER_AGENT_PROPERTY_NAME, ""))));
private static final Set<ConnectionProperty> INTERNAL_PROPERTIES =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList(
ConnectionProperty.createStringProperty(USER_AGENT_PROPERTY_NAME, ""))));
private static final Set<ConnectionProperty> INTERNAL_VALID_PROPERTIES =
Sets.union(VALID_PROPERTIES, INTERNAL_PROPERTIES);
/**
* Gets the default project-id for the current environment as defined by {@link
* ServiceOptions#getDefaultProjectId()}, and if none could be found, the project-id of the given
* credentials if it contains any.
*
* @param credentials The credentials to use to get the default project-id if none could be found
* in the environment.
* @return the default project-id.
*/
public static String getDefaultProjectId(Credentials credentials) {
String projectId = SpannerOptions.getDefaultProjectId();
if (projectId == null
&& credentials != null
&& credentials instanceof ServiceAccountCredentials) {
projectId = ((ServiceAccountCredentials) credentials).getProjectId();
}
return projectId;
}
/**
* Closes <strong>all</strong> {@link Spanner} instances that have been opened by connections
* during the lifetime of this JVM. Call this method at the end of your application to free up
* resources. You must close all {@link Connection}s that have been opened by your application
* before calling this method. Failing to do so, will cause this method to throw a {@link
* SpannerException}.
*
* <p>This method is also automatically called by a shutdown hook (see {@link
* Runtime#addShutdownHook(Thread)}) when the JVM is shutdown gracefully.
*/
public static void closeSpanner() {
SpannerPool.INSTANCE.checkAndCloseSpanners();
}
/** Builder for {@link ConnectionOptions} instances. */
public static class Builder {
private String uri;
private String credentialsUrl;
private String oauthToken;
private Credentials credentials;
private List<StatementExecutionInterceptor> statementExecutionInterceptors =
Collections.emptyList();
private Builder() {}
/** Spanner {@link ConnectionOptions} URI format. */
public static final String SPANNER_URI_FORMAT =
"(?:cloudspanner:)(?<HOSTGROUP>//[\\w.-]+(?:\\.[\\w\\.-]+)*[\\w\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=.]+)?/projects/(?<PROJECTGROUP>(([a-z]|[-]|[0-9])+|(DEFAULT_PROJECT_ID)))(/instances/(?<INSTANCEGROUP>([a-z]|[-]|[0-9])+)(/databases/(?<DATABASEGROUP>([a-z]|[-]|[_]|[0-9])+))?)?(?:[?|;].*)?";
private static final String SPANNER_URI_REGEX = "(?is)^" + SPANNER_URI_FORMAT + "$";
private static final Pattern SPANNER_URI_PATTERN = Pattern.compile(SPANNER_URI_REGEX);
private static final String HOST_GROUP = "HOSTGROUP";
private static final String PROJECT_GROUP = "PROJECTGROUP";
private static final String INSTANCE_GROUP = "INSTANCEGROUP";
private static final String DATABASE_GROUP = "DATABASEGROUP";
private static final String DEFAULT_PROJECT_ID_PLACEHOLDER = "DEFAULT_PROJECT_ID";
private boolean isValidUri(String uri) {
return SPANNER_URI_PATTERN.matcher(uri).matches();
}
/**
* Sets the URI of the Cloud Spanner database to connect to. A connection URI must be specified
* in this format:
*
* <pre>
* cloudspanner:[//host[:port]]/projects/project-id[/instances/instance-id[/databases/database-name]][\?property-name=property-value[;property-name=property-value]*]?
* </pre>
*
* The property-value strings should be url-encoded.
*
* <p>The project-id part of the URI may be filled with the placeholder DEFAULT_PROJECT_ID. This
* placeholder will be replaced by the default project id of the environment that is requesting
* a connection.
*
* <p>The supported properties are:
*
* <ul>
* <li>credentials (String): URL for the credentials file to use for the connection. This
* property is only used if no credentials have been specified using the {@link
* ConnectionOptions.Builder#setCredentialsUrl(String)} method. If you do not specify any
* credentials at all, the default credentials of the environment as returned by {@link
* GoogleCredentials#getApplicationDefault()} will be used.
* <li>autocommit (boolean): Sets the initial autocommit mode for the connection. Default is
* true.
* <li>readonly (boolean): Sets the initial readonly mode for the connection. Default is
* false.
* <li>retryAbortsInternally (boolean): Sets the initial retryAbortsInternally mode for the
* connection. Default is true.
* </ul>
*
* @param uri The URI of the Spanner database to connect to.
* @return this builder
*/
public Builder setUri(String uri) {
Preconditions.checkArgument(
isValidUri(uri),
"The specified URI is not a valid Cloud Spanner connection URI. Please specify a URI in the format \"cloudspanner:[//host[:port]]/projects/project-id[/instances/instance-id[/databases/database-name]][\\?property-name=property-value[;property-name=property-value]*]?\"");
checkValidProperties(uri);
this.uri = uri;
return this;
}
/**
* Sets the URL of the credentials file to use for this connection. The URL may be a reference
* to a file on the local file system, or to a file on Google Cloud Storage. References to
* Google Cloud Storage files are only allowed when the application is running on Google Cloud
* and the environment has access to the specified storage location. It also requires that the
* Google Cloud Storage client library is present on the class path. The Google Cloud Storage
* library is not automatically added as a dependency by the JDBC driver.
*
* <p>If you do not specify a credentialsUrl (either by using this setter, or by specifying on
* the connection URI), the credentials returned by {@link
* GoogleCredentials#getApplicationDefault()} will be used for the connection.
*
* @param credentialsUrl A valid file or Google Cloud Storage URL for the credentials file to be
* used.
* @return this builder
*/
public Builder setCredentialsUrl(String credentialsUrl) {
this.credentialsUrl = credentialsUrl;
return this;
}
/**
* Sets the OAuth token to use with this connection. The token must be a valid token with access
* to the resources (project/instance/database) that the connection will be accessing. This
* authentication method cannot be used in combination with a credentials file. If both an OAuth
* token and a credentials file is specified, the {@link #build()} method will throw an
* exception.
*
* @param oauthToken A valid OAuth token for the Google Cloud project that is used by this
* connection.
* @return this builder
*/
public Builder setOAuthToken(String oauthToken) {
this.oauthToken = oauthToken;
return this;
}
@VisibleForTesting
Builder setStatementExecutionInterceptors(List<StatementExecutionInterceptor> interceptors) {
this.statementExecutionInterceptors = interceptors;
return this;
}
@VisibleForTesting
Builder setCredentials(Credentials credentials) {
this.credentials = credentials;
return this;
}
/** @return the {@link ConnectionOptions} */
public ConnectionOptions build() {
Preconditions.checkState(this.uri != null, "Connection URI is required");
return new ConnectionOptions(this);
}
}
/**
* Create a {@link Builder} for {@link ConnectionOptions}. Use this method to create {@link
* ConnectionOptions} that can be used to obtain a {@link Connection}.
*
* @return a new {@link Builder}
*/
public static Builder newBuilder() {
return new Builder();
}
private final String uri;
private final String credentialsUrl;
private final String oauthToken;
private final boolean usePlainText;
private final String host;
private final String projectId;
private final String instanceId;
private final String databaseName;
private final Credentials credentials;
private final Integer numChannels;
private final String userAgent;
private final boolean autocommit;
private final boolean readOnly;
private final boolean retryAbortsInternally;
private final List<StatementExecutionInterceptor> statementExecutionInterceptors;
private ConnectionOptions(Builder builder) {
Matcher matcher = Builder.SPANNER_URI_PATTERN.matcher(builder.uri);
Preconditions.checkArgument(
matcher.find(), String.format("Invalid connection URI specified: %s", builder.uri));
checkValidProperties(builder.uri);
this.uri = builder.uri;
this.credentialsUrl =
builder.credentialsUrl != null ? builder.credentialsUrl : parseCredentials(builder.uri);
this.oauthToken =
builder.oauthToken != null ? builder.oauthToken : parseOAuthToken(builder.uri);
// Check that not both credentials and an OAuth token have been specified.
Preconditions.checkArgument(
(builder.credentials == null && this.credentialsUrl == null) || this.oauthToken == null,
"Cannot specify both credentials and an OAuth token.");
this.usePlainText = parseUsePlainText(this.uri);
this.userAgent = parseUserAgent(this.uri);
this.host =
matcher.group(Builder.HOST_GROUP) == null
? DEFAULT_HOST
: (usePlainText ? PLAIN_TEXT_PROTOCOL : HOST_PROTOCOL)
+ matcher.group(Builder.HOST_GROUP);
this.instanceId = matcher.group(Builder.INSTANCE_GROUP);
this.databaseName = matcher.group(Builder.DATABASE_GROUP);
// Using credentials on a plain text connection is not allowed, so if the user has not specified
// any credentials and is using a plain text connection, we should not try to get the
// credentials from the environment, but default to NoCredentials.
if (builder.credentials == null
&& this.credentialsUrl == null
&& this.oauthToken == null
&& this.usePlainText) {
this.credentials = NoCredentials.getInstance();
} else if (this.oauthToken != null) {
this.credentials = new GoogleCredentials(new AccessToken(oauthToken, null));
} else {
this.credentials =
builder.credentials == null
? getCredentialsService().createCredentials(this.credentialsUrl)
: builder.credentials;
}
String numChannelsValue = parseNumChannels(builder.uri);
if (numChannelsValue != null) {
try {
this.numChannels = Integer.valueOf(numChannelsValue);
} catch (NumberFormatException e) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"Invalid numChannels value specified: " + numChannelsValue,
e);
}
} else {
this.numChannels = null;
}
String projectId = matcher.group(Builder.PROJECT_GROUP);
if (Builder.DEFAULT_PROJECT_ID_PLACEHOLDER.equalsIgnoreCase(projectId)) {
projectId = getDefaultProjectId(this.credentials);
}
this.projectId = projectId;
this.autocommit = parseAutocommit(this.uri);
this.readOnly = parseReadOnly(this.uri);
this.retryAbortsInternally = parseRetryAbortsInternally(this.uri);
this.statementExecutionInterceptors =
Collections.unmodifiableList(builder.statementExecutionInterceptors);
}
@VisibleForTesting
CredentialsService getCredentialsService() {
return CredentialsService.INSTANCE;
}
@VisibleForTesting
static boolean parseUsePlainText(String uri) {
String value = parseUriProperty(uri, USE_PLAIN_TEXT_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_USE_PLAIN_TEXT;
}
@VisibleForTesting
static boolean parseAutocommit(String uri) {
String value = parseUriProperty(uri, AUTOCOMMIT_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_AUTOCOMMIT;
}
@VisibleForTesting
static boolean parseReadOnly(String uri) {
String value = parseUriProperty(uri, READONLY_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_READONLY;
}
@VisibleForTesting
static boolean parseRetryAbortsInternally(String uri) {
String value = parseUriProperty(uri, RETRY_ABORTS_INTERNALLY_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_RETRY_ABORTS_INTERNALLY;
}
@VisibleForTesting
static String parseCredentials(String uri) {
String value = parseUriProperty(uri, CREDENTIALS_PROPERTY_NAME);
return value != null ? value : DEFAULT_CREDENTIALS;
}
@VisibleForTesting
static String parseOAuthToken(String uri) {
String value = parseUriProperty(uri, OAUTH_TOKEN_PROPERTY_NAME);
return value != null ? value : DEFAULT_OAUTH_TOKEN;
}
@VisibleForTesting
static String parseNumChannels(String uri) {
String value = parseUriProperty(uri, NUM_CHANNELS_PROPERTY_NAME);
return value != null ? value : DEFAULT_NUM_CHANNELS;
}
@VisibleForTesting
static String parseUserAgent(String uri) {
String value = parseUriProperty(uri, USER_AGENT_PROPERTY_NAME);
return value != null ? value : DEFAULT_USER_AGENT;
}
@VisibleForTesting
static String parseUriProperty(String uri, String property) {
Pattern pattern = Pattern.compile(String.format("(?is)(?:;|\\?)%s=(.*?)(?:;|$)", property));
Matcher matcher = pattern.matcher(uri);
if (matcher.find() && matcher.groupCount() == 1) {
return matcher.group(1);
}
return null;
}
/** Check that only valid properties have been specified. */
@VisibleForTesting
static void checkValidProperties(String uri) {
String invalidProperties = "";
List<String> properties = parseProperties(uri);
for (String property : properties) {
if (!INTERNAL_VALID_PROPERTIES.contains(ConnectionProperty.createEmptyProperty(property))) {
if (invalidProperties.length() > 0) {
invalidProperties = invalidProperties + ", ";
}
invalidProperties = invalidProperties + property;
}
}
Preconditions.checkArgument(
invalidProperties.isEmpty(),
"Invalid properties found in connection URI: " + invalidProperties.toString());
}
@VisibleForTesting
static List<String> parseProperties(String uri) {
Pattern pattern = Pattern.compile("(?is)(?:\\?|;)(?<PROPERTY>.*?)=(?:.*?)");
Matcher matcher = pattern.matcher(uri);
List<String> res = new ArrayList<>();
while (matcher.find() && matcher.group("PROPERTY") != null) {
res.add(matcher.group("PROPERTY"));
}
return res;
}
/**
* Create a new {@link Connection} from this {@link ConnectionOptions}. Calling this method
* multiple times for the same {@link ConnectionOptions} will return multiple instances of {@link
* Connection}s to the same database.
*
* @return a new {@link Connection} to the database referenced by this {@link ConnectionOptions}
*/
public Connection getConnection() {
return new ConnectionImpl(this);
}
/** The URI of this {@link ConnectionOptions} */
public String getUri() {
return uri;
}
/** The credentials URL of this {@link ConnectionOptions} */
public String getCredentialsUrl() {
return credentialsUrl;
}
/** The number of channels to use for the connection. */
public Integer getNumChannels() {
return numChannels;
}
/** The host and port number that this {@link ConnectionOptions} will connect to */
public String getHost() {
return host;
}
/** The Google Project ID that this {@link ConnectionOptions} will connect to */
public String getProjectId() {
return projectId;
}
/** The Spanner Instance ID that this {@link ConnectionOptions} will connect to */
public String getInstanceId() {
return instanceId;
}
/** The Spanner database name that this {@link ConnectionOptions} will connect to */
public String getDatabaseName() {
return databaseName;
}
/** The Spanner {@link DatabaseId} that this {@link ConnectionOptions} will connect to */
public DatabaseId getDatabaseId() {
Preconditions.checkState(projectId != null, "Project ID is not specified");
Preconditions.checkState(instanceId != null, "Instance ID is not specified");
Preconditions.checkState(databaseName != null, "Database name is not specified");
return DatabaseId.of(projectId, instanceId, databaseName);
}
/**
* The {@link Credentials} of this {@link ConnectionOptions}. This is either the credentials
* specified in the credentialsUrl or the default Google application credentials
*/
public Credentials getCredentials() {
return credentials;
}
/** The initial autocommit value for connections created by this {@link ConnectionOptions} */
public boolean isAutocommit() {
return autocommit;
}
/** The initial readonly value for connections created by this {@link ConnectionOptions} */
public boolean isReadOnly() {
return readOnly;
}
/**
* The initial retryAbortsInternally value for connections created by this {@link
* ConnectionOptions}
*/
public boolean isRetryAbortsInternally() {
return retryAbortsInternally;
}
/** Use http instead of https. Only valid for (local) test servers. */
boolean isUsePlainText() {
return usePlainText;
}
/**
* The (custom) user agent string to use for this connection. If <code>null</code>, then the
* default JDBC user agent string will be used.
*/
String getUserAgent() {
return userAgent;
}
/** Interceptors that should be executed after each statement */
List<StatementExecutionInterceptor> getStatementExecutionInterceptors() {
return statementExecutionInterceptors;
}
@Override
public String toString() {
return getUri();
}
}