Skip to content

Commit 29a5c5a

Browse files
Create staementfactory on-demand
1 parent eebf2d0 commit 29a5c5a

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,6 @@ ServerStream<BatchWriteResponse> batchWriteAtLeastOnce(
617617
* <p>databaseClient.newStatementFactory().of("SELECT NAME FROM TABLE WHERE ID = ?", 10)
618618
*/
619619
default StatementFactory newStatementFactory() {
620-
return new StatementFactory(getDialect());
620+
throw new UnsupportedOperationException("method should be overwritten");
621621
}
622622
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@
2222
import com.google.cloud.spanner.Options.UpdateOption;
2323
import com.google.cloud.spanner.SessionPool.PooledSessionFuture;
2424
import com.google.cloud.spanner.SpannerImpl.ClosedException;
25+
import com.google.cloud.spanner.Statement.StatementFactory;
2526
import com.google.common.annotations.VisibleForTesting;
2627
import com.google.common.base.Function;
2728
import com.google.common.util.concurrent.ListenableFuture;
2829
import com.google.spanner.v1.BatchWriteResponse;
2930
import io.opentelemetry.api.common.Attributes;
31+
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.Future;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
3035
import javax.annotation.Nullable;
3136

3237
class DatabaseClientImpl implements DatabaseClient {
@@ -41,6 +46,8 @@ class DatabaseClientImpl implements DatabaseClient {
4146
@VisibleForTesting final boolean useMultiplexedSessionPartitionedOps;
4247
@VisibleForTesting final boolean useMultiplexedSessionForRW;
4348

49+
private StatementFactory statementFactory = null;
50+
4451
final boolean useMultiplexedSessionBlindWrite;
4552

4653
@VisibleForTesting
@@ -139,6 +146,21 @@ public Dialect getDialect() {
139146
return pool.getDialect();
140147
}
141148

149+
@Override
150+
public StatementFactory newStatementFactory() {
151+
if (statementFactory == null) {
152+
try {
153+
Dialect dialect = getDialectAsync().get(5, TimeUnit.SECONDS);
154+
statementFactory = new StatementFactory(dialect);
155+
} catch (ExecutionException | TimeoutException e) {
156+
throw SpannerExceptionFactory.asSpannerException(e);
157+
} catch (InterruptedException e) {
158+
throw SpannerExceptionFactory.propagateInterrupt(e);
159+
}
160+
}
161+
return statementFactory;
162+
}
163+
142164
@Override
143165
@Nullable
144166
public String getDatabaseRole() {
@@ -346,6 +368,14 @@ public long executePartitionedUpdate(final Statement stmt, final UpdateOption...
346368
return executePartitionedUpdateWithPooledSession(stmt, options);
347369
}
348370

371+
private Future<Dialect> getDialectAsync() {
372+
MultiplexedSessionDatabaseClient client = getMultiplexedSessionDatabaseClient();
373+
if (client != null) {
374+
return client.getDialectAsync();
375+
}
376+
return pool.getDialectAsync();
377+
}
378+
349379
private long executePartitionedUpdateWithPooledSession(
350380
final Statement stmt, final UpdateOption... options) {
351381
ISpan span = tracer.spanBuilder(PARTITION_DML_TRANSACTION, commonAttributes);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.Map;
4545
import java.util.concurrent.ExecutionException;
4646
import java.util.concurrent.Executors;
47+
import java.util.concurrent.Future;
4748
import java.util.concurrent.ScheduledExecutorService;
4849
import java.util.concurrent.ScheduledFuture;
4950
import java.util.concurrent.TimeUnit;
@@ -652,6 +653,14 @@ public Dialect getDialect() {
652653
}
653654
}
654655

656+
public Future<Dialect> getDialectAsync() {
657+
try {
658+
return MAINTAINER_SERVICE.submit(dialectSupplier::get);
659+
} catch (Exception exception) {
660+
throw SpannerExceptionFactory.asSpannerException(exception);
661+
}
662+
}
663+
655664
@Override
656665
public Timestamp write(Iterable<Mutation> mutations) throws SpannerException {
657666
return createMultiplexedSessionTransaction(/* singleUse = */ false).write(mutations);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
import java.util.concurrent.CountDownLatch;
105105
import java.util.concurrent.ExecutionException;
106106
import java.util.concurrent.Executor;
107+
import java.util.concurrent.Future;
107108
import java.util.concurrent.ScheduledExecutorService;
108109
import java.util.concurrent.ScheduledFuture;
109110
import java.util.concurrent.TimeUnit;
@@ -2548,6 +2549,10 @@ Dialect getDialect() {
25482549
}
25492550
}
25502551

2552+
Future<Dialect> getDialectAsync() {
2553+
return executor.submit(this::getDialect);
2554+
}
2555+
25512556
PooledSessionReplacementHandler getPooledSessionReplacementHandler() {
25522557
return pooledSessionReplacementHandler;
25532558
}

0 commit comments

Comments
 (0)