From d36195baf327755976a4800953512002c83bed37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Mon, 28 Aug 2023 15:23:55 +0200 Subject: [PATCH 1/2] docs: include dependencies in sample README --- .github/release-please.yml | 2 +- samples/java/jdbc/README.md | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/release-please.yml b/.github/release-please.yml index bdafab20c..0418d39a2 100644 --- a/.github/release-please.yml +++ b/.github/release-please.yml @@ -2,4 +2,4 @@ primaryBranch: postgresql-dialect releaseType: java-yoshi bumpMinorPreMajor: true handleGHRelease: true -extraFiles: ["README.md"] +extraFiles: ["README.md", "samples/java/jdbc/README.md"] diff --git a/samples/java/jdbc/README.md b/samples/java/jdbc/README.md index 359f99207..b9ad6278f 100644 --- a/samples/java/jdbc/README.md +++ b/samples/java/jdbc/README.md @@ -4,6 +4,25 @@ This sample application shows how to connect to Cloud Spanner through PGAdapter `JDBC` PostgreSQL driver. PGAdapter is automatically started in-process together with the sample application. +The sample application adds the following dependencies: + + +```xml + + + org.postgresql + postgresql + 42.6.0 + + + com.google.cloud + google-cloud-spanner-pgadapter + 0.23.0 + + +``` + + Run the sample with the following command: ```shell From 2c3e33ac10eb35e565ff4c8d35b23f00ed6ae0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Mon, 28 Aug 2023 15:30:00 +0200 Subject: [PATCH 2/2] docs: also add sample code to the README --- samples/java/jdbc/README.md | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/samples/java/jdbc/README.md b/samples/java/jdbc/README.md index b9ad6278f..cb68e3977 100644 --- a/samples/java/jdbc/README.md +++ b/samples/java/jdbc/README.md @@ -4,6 +4,8 @@ This sample application shows how to connect to Cloud Spanner through PGAdapter `JDBC` PostgreSQL driver. PGAdapter is automatically started in-process together with the sample application. +### Dependencies + The sample application adds the following dependencies: @@ -23,6 +25,48 @@ The sample application adds the following dependencies: ``` +### Connecting +PGAdapter is started in-process with the sample application: + +```java + +OptionsMetadata.Builder builder = + OptionsMetadata.newBuilder() + .setProject(project) + .setInstance(instance) + // Start PGAdapter on any available port. + .setPort(0); +ProxyServer server = new ProxyServer(builder.build()); +server.startServer(); +server.awaitRunning(); + +``` + +The PostgreSQL JDBC driver can connect to the in-process PGAdapter instance. This example uses +Unix Domain Sockets for the lowest possible latency: + +```java + +// Create a connection URL that will use Unix domain sockets to connect to PGAdapter. +String connectionUrl = + String.format( + "jdbc:postgresql://localhost/%s?" + + "socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg" + + "&socketFactoryArg=/tmp/.s.PGSQL.%d", + database, server.getLocalPort()); +try (Connection connection = DriverManager.getConnection(connectionUrl)) { + try (ResultSet resultSet = + connection.createStatement().executeQuery("select 'Hello World!' as greeting")) { + while (resultSet.next()) { + System.out.printf("\nGreeting: %s\n\n", resultSet.getString("greeting")); + } + } +} + +``` + +### Run Sample + Run the sample with the following command: ```shell