|
| 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] |
0 commit comments