private static Spanner getSpanner(Properties properties, String host, String project) { if (spanner != null) { return spanner; } String numChannels = properties.getProperty(CloudSpannerProperties.NUM_CHANNELS); int numThreads = Integer.parseInt(properties.getProperty(Client.THREAD_COUNT_PROPERTY, "1")); SpannerOptions.Builder optionsBuilder = SpannerOptions.newBuilder() .setSessionPoolOption(SessionPoolOptions.newBuilder() .setMinSessions(numThreads) // Since we have no read-write transactions, we can set the write session fraction to 0. .setWriteSessionsFraction(0) .build()); if (host != null) { optionsBuilder.setHost(host); } if (project != null) { optionsBuilder.setProjectId(project); } if (numChannels != null) { optionsBuilder.setNumChannels(Integer.parseInt(numChannels)); } spanner = optionsBuilder.build().getService(); Runtime.getRuntime().addShutdownHook(new Thread("spannerShutdown") { @Override public void run() { spanner.close(); } }); return spanner; }
@Bean @ConditionalOnMissingBean public SpannerOptions spannerOptions(SessionPoolOptions sessionPoolOptions) { Builder builder = SpannerOptions.newBuilder() .setProjectId(this.projectId) .setHeaderProvider(new UserAgentHeaderProvider(this.getClass())) .setCredentials(this.credentials); if (this.numRpcChannels >= 0) { builder.setNumChannels(this.numRpcChannels); } if (this.prefetchChunks >= 0) { builder.setPrefetchChunks(this.prefetchChunks); } builder.setSessionPoolOption(sessionPoolOptions); return builder.build(); }