Skip to content

Commit db19727

Browse files
authored
docs: add region tags for hikaricp config (#86)
1 parent f21987a commit db19727

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
package com.google.cloud.alloydb;
17+
18+
// [START alloydb_hikaricp_connect_connector]
19+
import com.zaxxer.hikari.HikariConfig;
20+
import com.zaxxer.hikari.HikariDataSource;
21+
22+
public class AlloyDbJdbcConnectorDataSourceFactory {
23+
24+
public static final String ALLOYDB_USER = System.getenv("ALLOYDB_USER");
25+
public static final String ALLOYDB_PASS = System.getenv("ALLOYDB_PASS");
26+
public static final String ALLOYDB_INSTANCE_NAME = System.getenv("ALLOYDB_INSTANCE_NAME");
27+
28+
static HikariDataSource createDataSource() {
29+
HikariConfig config = new HikariConfig();
30+
31+
config.setJdbcUrl("jdbc:postgresql:///postgres");
32+
config.setUsername(ALLOYDB_USER); // e.g., "postgres"
33+
config.setPassword(ALLOYDB_PASS); // e.g., "secret-password"
34+
config.addDataSourceProperty("socketFactory", "com.google.cloud.alloydb.SocketFactory");
35+
// e.g., "projects/my-project/locations/us-central1/clusters/my-cluster/instances/my-instance"
36+
config.addDataSourceProperty("alloydbInstanceName", ALLOYDB_INSTANCE_NAME);
37+
38+
return new HikariDataSource(config);
39+
}
40+
}
41+
// [END alloydb_hikaricp_connect_connector]

alloydb-jdbc-connector/src/test/java/com/google/cloud/alloydb/ITSocketFactoryTest.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.google.common.collect.BoundType;
2121
import com.google.common.collect.Range;
22-
import com.zaxxer.hikari.HikariConfig;
2322
import com.zaxxer.hikari.HikariDataSource;
2423
import java.sql.Connection;
2524
import java.sql.PreparedStatement;
@@ -38,15 +37,7 @@ public class ITSocketFactoryTest {
3837

3938
@Before
4039
public void setUp() {
41-
HikariConfig config = new HikariConfig();
42-
43-
config.setJdbcUrl("jdbc:postgresql:///postgres");
44-
config.setUsername(System.getenv("ALLOYDB_USER"));
45-
config.setPassword(System.getenv("ALLOYDB_PASS"));
46-
config.addDataSourceProperty("socketFactory", "com.google.cloud.alloydb.SocketFactory");
47-
config.addDataSourceProperty("alloydbInstanceName", System.getenv("ALLOYDB_INSTANCE_NAME"));
48-
49-
this.dataSource = new HikariDataSource(config);
40+
this.dataSource = AlloyDbJdbcConnectorDataSourceFactory.createDataSource();
5041
}
5142

5243
@After

0 commit comments

Comments
 (0)