Skip to content

Commit a651565

Browse files
committed
use shouldRefreshService instead of createNewService
1 parent 75d68e7 commit a651565

3 files changed

Lines changed: 32 additions & 30 deletions

File tree

google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -494,20 +494,18 @@ static String getServiceAccountProjectId(String credentialsPath) {
494494
*/
495495
@SuppressWarnings("unchecked")
496496
public ServiceT getService() {
497-
if (service == null) {
497+
if (shouldRefreshService(service)) {
498498
service = serviceFactory.create((OptionsT) this);
499499
}
500500
return service;
501501
}
502502

503503
/**
504-
* Creates a new service object and returns it. Subsequent calls to {@link #getService()} will
505-
* return this instance.
504+
* @param cachedService The currently cached service object
505+
* @return true if the currently cached service object should be refreshed.
506506
*/
507-
@SuppressWarnings("unchecked")
508-
protected ServiceT createNewService() {
509-
service = serviceFactory.create((OptionsT) this);
510-
return service;
507+
protected boolean shouldRefreshService(ServiceT cachedService) {
508+
return cachedService == null;
511509
}
512510

513511
/**
@@ -516,20 +514,18 @@ protected ServiceT createNewService() {
516514
*/
517515
@SuppressWarnings("unchecked")
518516
public ServiceRpc getRpc() {
519-
if (rpc == null) {
517+
if (shouldRefreshRpc(rpc)) {
520518
rpc = serviceRpcFactory.create((OptionsT) this);
521519
}
522520
return rpc;
523521
}
524522

525523
/**
526-
* Creates a new rpc object and returns it. Subsequent calls to {@link #getRpc()} will return this
527-
* instance.
524+
* @param cachedService The currently cached service object
525+
* @return true if the currently cached service object should be refreshed.
528526
*/
529-
@SuppressWarnings("unchecked")
530-
protected ServiceRpc createNewRpc() {
531-
rpc = serviceRpcFactory.create((OptionsT) this);
532-
return rpc;
527+
protected boolean shouldRefreshRpc(ServiceRpc cachedRpc) {
528+
return cachedRpc == null;
533529
}
534530

535531
/**

google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public Set<String> getScopes() {
295295
}
296296

297297
protected SpannerRpc getSpannerRpcV1() {
298-
return (SpannerRpc) getRpc();
298+
return getRpc();
299299
}
300300

301301
/**
@@ -306,11 +306,13 @@ protected SpannerRpc getSpannerRpcV1() {
306306
*/
307307
@Override
308308
public Spanner getService() {
309-
Spanner spanner = super.getService();
310-
if (spanner.isClosed()) {
311-
spanner = createNewService();
312-
}
313-
return spanner;
309+
// Method is only overridden in order to supply additional documentation.
310+
return super.getService();
311+
}
312+
313+
@Override
314+
protected boolean shouldRefreshService(Spanner cachedService) {
315+
return cachedService == null || cachedService.isClosed();
314316
}
315317

316318
/**
@@ -320,12 +322,14 @@ public Spanner getService() {
320322
* ServiceRpc} instance.
321323
*/
322324
@Override
323-
public ServiceRpc getRpc() {
324-
SpannerRpc rpc = (SpannerRpc) super.getRpc();
325-
if (rpc.isClosed()) {
326-
rpc = (SpannerRpc) createNewRpc();
327-
}
328-
return rpc;
325+
public SpannerRpc getRpc() {
326+
// Method is only overridden in order to supply additional documentation.
327+
return (SpannerRpc) super.getRpc();
328+
}
329+
330+
@Override
331+
protected boolean shouldRefreshRpc(ServiceRpc cachedRpc) {
332+
return cachedRpc == null || ((SpannerRpc) cachedRpc).isClosed();
329333
}
330334

331335
@SuppressWarnings("unchecked")

google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.hamcrest.CoreMatchers.is;
2121
import static org.junit.Assert.assertThat;
22+
2223
import com.google.cloud.NoCredentials;
2324
import com.google.cloud.TransportOptions;
2425
import java.util.HashMap;
@@ -87,10 +88,11 @@ public void testNullSessionLabels() {
8788

8889
@Test
8990
public void testDoNotCacheClosedSpannerInstance() {
90-
SpannerOptions options = SpannerOptions.newBuilder()
91-
.setProjectId("[PROJECT]")
92-
.setCredentials(NoCredentials.getInstance())
93-
.build();
91+
SpannerOptions options =
92+
SpannerOptions.newBuilder()
93+
.setProjectId("[PROJECT]")
94+
.setCredentials(NoCredentials.getInstance())
95+
.build();
9496
// Getting a service twice should give the same instance.
9597
Spanner service1 = options.getService();
9698
Spanner service2 = options.getService();

0 commit comments

Comments
 (0)