Skip to content

Commit fd47916

Browse files
committed
feat: Add sample code for connection pooling
1 parent 4eeeb4f commit fd47916

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore;
18+
19+
// [START datastore_configure_connection_pool]
20+
21+
import com.google.api.gax.grpc.ChannelPoolSettings;
22+
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
23+
import com.google.cloud.datastore.DatastoreOptions;
24+
import com.google.cloud.datastore.v1.DatastoreSettings;
25+
import com.google.cloud.grpc.GrpcTransportOptions;
26+
27+
28+
public class ConfigureConnectionPool {
29+
30+
public static void main(String... args) throws Exception {
31+
InstantiatingGrpcChannelProvider channelProvider =
32+
DatastoreSettings.defaultGrpcTransportProviderBuilder()
33+
.setChannelPoolSettings(
34+
ChannelPoolSettings.builder()
35+
.setInitialChannelCount(10)
36+
.setMinChannelCount(5)
37+
.setMaxChannelCount(200)
38+
.build())
39+
.build();
40+
41+
DatastoreOptions datastoreOptions = DatastoreOptions.newBuilder()
42+
.setProjectId("my-project")
43+
.setChannelProvider(channelProvider)
44+
.setTransportOptions(GrpcTransportOptions.newBuilder().build())
45+
.build();
46+
47+
ChannelPoolSettings channelPoolSettings =
48+
((InstantiatingGrpcChannelProvider) datastoreOptions.getTransportChannelProvider())
49+
.getChannelPoolSettings();
50+
51+
System.out.printf(String.format(
52+
"Connected with pool with InitialChannelCount: %d, MinChannelCount: %d, MaxChannelCount: %d",
53+
channelPoolSettings.getInitialChannelCount(), channelPoolSettings.getMinChannelCount(),
54+
channelPoolSettings.getMaxChannelCount()
55+
));
56+
}
57+
}
58+
59+
// [END datastore_configure_connection_pool]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2016 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore;
18+
19+
import com.rule.SystemsOutRule;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.JUnit4;
24+
25+
/**
26+
* Tests for ConfigureConnectionPool sample.
27+
*/
28+
@RunWith(JUnit4.class)
29+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
30+
public class ConfigureConnectionPoolIT {
31+
32+
@Rule
33+
public final SystemsOutRule systemsOutRule = new SystemsOutRule();
34+
35+
@Test
36+
public void testConfigureConnectionPool() throws Exception {
37+
ConfigureConnectionPool.main();
38+
systemsOutRule.assertContains(
39+
"Connected with pool with InitialChannelCount: 10, MinChannelCount: 5, MaxChannelCount: 200");
40+
}
41+
}

0 commit comments

Comments
 (0)