Skip to content

Commit 196663c

Browse files
authored
Update pgJDBC example to include non-admin and connection refresh logic (#112)
* Update pgJDBC sample * Fix license * Remove clean up * Fix gradle dependencies * Re-add gradle test deps --------- Co-authored-by: Wilson de Carvalho <wilspmor@amazon.com>
1 parent 9b93f66 commit 196663c

8 files changed

Lines changed: 235 additions & 84 deletions

File tree

.github/workflows/java-pgjdbc-integ-tests.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ jobs:
4848
role-to-assume: ${{ secrets.JAVA_IAM_ROLE }}
4949
aws-region: us-east-1
5050

51-
- name: Configure and run integration for pgjdbc
51+
- name: Configure and run integration for pgjdbc - admin
5252
working-directory: ./java/pgjdbc
5353
env:
5454
CLUSTER_ENDPOINT: ${{ secrets.JAVA_PGJDBC_CLUSTER_ENDPOINT }}
55+
CLUSTER_USER: admin
5556
REGION: ${{ secrets.JAVA_PGJDBC_CLUSTER_REGION }}
5657
run: |
5758
mvn validate

java/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
**/libs/software/
1212
**/gradlew
1313
**/gradlew.bat
14-
**/build.gradle.kts
15-
**/settings.gradle.kts
1614

1715
# Compiled class file
1816
*.class

java/pgjdbc/README.md

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,112 @@
1-
# Aurora DSQL pgJDBC code examples
1+
# pgJDBC with Aurora DSQL
22

33
## Overview
44

5-
The code examples in this topic show you how to use the Java to work with Aurora DSQL.
5+
This code example demonstrates how to use `pgJDBC` with Amazon Aurora DSQL.
6+
The example shows you how to connect to an Aurora DSQL cluster and perform basic database operations.
67

7-
## Run the examples
8+
Aurora DSQL is a distributed SQL database service that provides high availability and scalability for
9+
your PostgreSQL-compatible applications. `pgJDBC` is a popular PostgreSQL adapter for Java that allows
10+
you to interact with PostgreSQL databases using Java code.
11+
12+
## About the code example
13+
14+
The example demonstrates a flexible connection approach that works for both admin and non-admin users:
15+
16+
* When connecting as an **admin user**, the example uses the `public` schema and generates an admin authentication
17+
token.
18+
* When connecting as a **non-admin user**, the example uses a custom `myschema` schema and generates a standard
19+
authentication token.
20+
21+
The code automatically detects the user type and adjusts its behavior accordingly.
22+
23+
## ⚠️ Important
24+
25+
* Running this code might result in charges to your AWS account.
26+
* We recommend that you grant your code least privilege. At most, grant only the
27+
minimum permissions required to perform the task. For more information, see
28+
[Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
29+
* This code is not tested in every AWS Region. For more information, see
30+
[AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
31+
32+
## Run the example
833

934
### Prerequisites
1035

11-
- java version >= 17 is needed
36+
* You must have an AWS account, and have your default credentials and AWS Region
37+
configured as described in the
38+
[Globally configuring AWS SDKs and tools](https://docs.aws.amazon.com/credref/latest/refdocs/creds-config-files.html)
39+
guide.
40+
* Java Development Kit (JDK): Ensure you have JDK 17+ installed.
41+
42+
_To verify the java is installed, you can run_
43+
```bash
44+
java -version
45+
46+
* Build Tool (Maven or Gradle)
47+
- _Maven_: Ensure Maven is installed if that is your preferred option. You can download it from the [official website](https://maven.apache.org/download.cgi).
48+
- _Gradle_: Ensure Gradle is installed if that is your preferred option. You can download it from the [official website](https://gradle.org/install/).
49+
* AWS SDK: Ensure that you setup the latest version of the AWS Java SDK [official website](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html)
50+
* You must have an Aurora DSQL cluster. For information about creating an Aurora DSQL cluster, see the
51+
[Getting started with Aurora DSQL](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/getting-started.html)
52+
guide.
53+
* If connecting as a non-admin user, ensure the user is linked to an IAM role and is granted access to the `myschema`
54+
schema. See the
55+
[Using database roles with IAM roles](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/using-database-and-iam-roles.html)
56+
guide.
57+
58+
### Run the code
59+
60+
The example demonstrates the following operations:
61+
62+
- Opening a connection to an Aurora DSQL cluster
63+
- Creating a table
64+
- Inserting and querying data
65+
66+
The example is designed to work with both admin and non-admin users:
1267

13-
### Run the example tests
68+
- When run as an admin user, it uses the `public` schema
69+
- When run as a non-admin user, it uses the `myschema` schema
1470

15-
```sh
16-
# Use the account credentials dedicated for javascript
17-
export CLUSTER_ENDPOINT="<your cluster endpoint>"
18-
export REGION="<your cluster region>"
19-
mvn test
71+
**Note:** running the example will use actual resources in your AWS account and may incur charges.
72+
73+
Set environment variables for your cluster details:
74+
75+
```bash
76+
# e.g. "admin"
77+
export CLUSTER_USER="<your user>"
78+
79+
# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws"
80+
export CLUSTER_ENDPOINT="<your endpoint>"
81+
82+
# e.g. "us-east-1"
83+
export REGION="<your region>"
2084
```
2185

86+
Run the example:
87+
88+
- _Maven_:
89+
90+
```bash
91+
mvn compile assembly:single
92+
java -ea -jar target/AuroraDSQLExample-1.0-SNAPSHOT-jar-with-dependencies.jar
93+
```
94+
95+
- _Gradle_:
96+
97+
```bash
98+
gradle run
99+
```
100+
101+
102+
The example contains comments explaining the code and the operations being performed.
103+
104+
## Additional resources
105+
106+
* [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html)
107+
* [pgJDBC Documentation](https://jdbc.postgresql.org/documentation/)
108+
* [AWS SDK for Java Documentation](https://docs.aws.amazon.com/sdk-for-java/)
109+
22110
---
23111

24112
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

java/pgjdbc/build.gradle.kts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
2+
3+
plugins {
4+
id("java")
5+
id("application")
6+
}
7+
8+
application {
9+
mainClass = "org.example.Example"
10+
}
11+
12+
group = "org.example"
13+
version = "1.0-SNAPSHOT"
14+
15+
repositories {
16+
mavenCentral()
17+
}
18+
19+
dependencies {
20+
testImplementation(platform("org.junit:junit-bom:5.10.0"))
21+
testImplementation("org.junit.jupiter:junit-jupiter")
22+
implementation("org.postgresql:postgresql:42.7.4")
23+
implementation("software.amazon.awssdk:dsql:2.31.32")
24+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
25+
implementation("org.slf4j:slf4j-api:2.0.17")
26+
implementation("org.slf4j:slf4j-simple:2.0.17")
27+
}
28+
29+
tasks.test {
30+
useJUnitPlatform()
31+
32+
testLogging {
33+
events("passed", "skipped", "failed", "standardOut", "standardError")
34+
exceptionFormat = TestExceptionFormat.FULL
35+
}
36+
}
37+
38+
tasks.withType<Test> {
39+
this.testLogging {
40+
this.showStandardStreams = true
41+
}
42+
}
43+
44+
tasks.withType<JavaExec> {
45+
this.enableAssertions = true
46+
}

java/pgjdbc/pom.xml

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
<version>1.0-SNAPSHOT</version>
1010

1111
<properties>
12-
<maven.compiler.source>1.8</maven.compiler.source>
13-
<maven.compiler.target>1.8</maven.compiler.target>
12+
<maven.compiler.release>17</maven.compiler.release>
1413
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1514
<mainClass>org.example.Example</mainClass>
15+
<aws.sdk.version>2.31.32</aws.sdk.version>
16+
<slf4j.version>2.0.17</slf4j.version>
17+
<trimStackTrace>false</trimStackTrace>
1618
</properties>
1719

1820
<dependencies>
@@ -30,29 +32,39 @@
3032
<dependency>
3133
<groupId>software.amazon.awssdk</groupId>
3234
<artifactId>dsql</artifactId>
33-
<version>2.29.27</version>
35+
<version>${aws.sdk.version}</version>
3436
</dependency>
3537
<dependency>
3638
<groupId>software.amazon.awssdk</groupId>
3739
<artifactId>regions</artifactId>
38-
<version>2.25.60</version>
40+
<version>${aws.sdk.version}</version>
3941
</dependency>
4042
<dependency>
4143
<groupId>software.amazon.awssdk</groupId>
4244
<artifactId>aws-core</artifactId>
43-
<version>2.25.60</version>
45+
<version>${aws.sdk.version}</version>
4446
</dependency>
4547
<dependency>
4648
<groupId>software.amazon.awssdk</groupId>
4749
<artifactId>aws-json-protocol</artifactId>
48-
<version>2.25.60</version>
50+
<version>${aws.sdk.version}</version>
4951
</dependency>
5052
<dependency>
5153
<groupId>software.amazon.awssdk</groupId>
5254
<artifactId>url-connection-client</artifactId>
53-
<version>2.25.60</version>
55+
<version>${aws.sdk.version}</version>
5456
<scope>compile</scope>
5557
</dependency>
58+
<dependency>
59+
<groupId>org.slf4j</groupId>
60+
<artifactId>slf4j-api</artifactId>
61+
<version>${slf4j.version}</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.slf4j</groupId>
65+
<artifactId>slf4j-simple</artifactId>
66+
<version>${slf4j.version}</version>
67+
</dependency>
5668
</dependencies>
5769

5870
<build>
@@ -75,56 +87,33 @@
7587
</archive>
7688
</configuration>
7789
</plugin>
78-
<plugin>
79-
<groupId>org.codehaus.mojo</groupId>
80-
<artifactId>exec-maven-plugin</artifactId>
81-
<version>3.0.0</version>
82-
<configuration>
83-
<mainClass>${mainClass}</mainClass>
84-
<arguments>
85-
<argument>-Xms256m</argument>
86-
<argument>-Xmx512m</argument>
87-
</arguments>
88-
</configuration>
89-
</plugin>
90-
<plugin>
91-
<groupId>org.apache.maven.plugins</groupId>
92-
<artifactId>maven-jar-plugin</artifactId>
93-
<version>3.1.0</version>
94-
<configuration>
95-
<archive>
96-
<manifest>
97-
<addClasspath>true</addClasspath>
98-
<mainClass>org.example.Example</mainClass>
99-
</manifest>
100-
</archive>
101-
</configuration>
102-
</plugin>
10390
<!-- Add the assemble plugin with standard configuration -->
10491
<plugin>
10592
<artifactId>maven-assembly-plugin</artifactId>
10693
<configuration>
10794
<archive>
10895
<manifest>
109-
<mainClass>org.example.Example</mainClass>
96+
<mainClass>${mainClass}</mainClass>
11097
</manifest>
11198
</archive>
11299
<descriptorRefs>
113100
<descriptorRef>jar-with-dependencies</descriptorRef>
114101
</descriptorRefs>
115102
</configuration>
116103
</plugin>
117-
<plugin>
118-
<groupId>org.apache.maven.plugins</groupId>
119-
<artifactId>maven-surefire-plugin</artifactId>
120-
<version>3.0.0-M5</version>
121-
<configuration>
122-
<includes>
123-
<include>**/*Test.java</include>
124-
</includes>
125-
</configuration>
126-
</plugin>
127104
</plugins>
128105
</build>
106+
<repositories>
107+
<repository>
108+
<id>local-maven-repository</id>
109+
<url>file://${local.repository.folder}</url>
110+
<releases>
111+
<enabled>true</enabled>
112+
</releases>
113+
<snapshots>
114+
<enabled>true</enabled>
115+
</snapshots>
116+
</repository>
117+
</repositories>
129118
</project>
130119

java/pgjdbc/settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = "DSQLPGJDBCPetClinicExample"
2+

0 commit comments

Comments
 (0)