|
| 1 | +package org.example.alternatives.no_connection_pool; |
| 2 | + |
| 3 | +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; |
| 4 | +import software.amazon.awssdk.services.dsql.DsqlUtilities; |
| 5 | +import software.amazon.awssdk.services.dsql.model.GenerateAuthTokenRequest; |
| 6 | +import software.amazon.awssdk.regions.Region; |
| 7 | + |
| 8 | +import java.sql.Connection; |
| 9 | +import java.sql.DriverManager; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.sql.Statement; |
| 13 | +import java.util.Properties; |
| 14 | + |
| 15 | +public class ExampleWithNoConnector { |
| 16 | + |
| 17 | + // Get a connection to Aurora DSQL. |
| 18 | + public static Connection getConnection(String endpoint, String user, String region) throws SQLException { |
| 19 | + DsqlUtilities utilities = DsqlUtilities.builder() |
| 20 | + .region(Region.of(region)) |
| 21 | + .credentialsProvider(DefaultCredentialsProvider.create()) |
| 22 | + .build(); |
| 23 | + |
| 24 | + // The token expiration time is optional, and the default value is 900 seconds |
| 25 | + GenerateAuthTokenRequest tokenGenerator = GenerateAuthTokenRequest.builder() |
| 26 | + .hostname(endpoint) |
| 27 | + .region(Region.of(region)) |
| 28 | + .build(); |
| 29 | + |
| 30 | + // Generate a fresh password token for each connection, to ensure the token is |
| 31 | + // not expired when the connection is established |
| 32 | + String password; |
| 33 | + if (user.equals("admin")) { |
| 34 | + password = utilities.generateDbConnectAdminAuthToken(tokenGenerator); |
| 35 | + } else { |
| 36 | + password = utilities.generateDbConnectAuthToken(tokenGenerator); |
| 37 | + } |
| 38 | + |
| 39 | + Properties props = new Properties(); |
| 40 | + props.setProperty("user", user); |
| 41 | + props.setProperty("password", password); |
| 42 | + // Use the DefaultJavaSSLFactory so that Java's default trust store can be used |
| 43 | + // to verify the server's root cert. |
| 44 | + props.setProperty("sslmode", "verify-full"); |
| 45 | + props.setProperty("sslfactory", "org.postgresql.ssl.DefaultJavaSSLFactory"); |
| 46 | + props.setProperty("sslNegotiation", "direct"); |
| 47 | + |
| 48 | + String url = "jdbc:postgresql://" + endpoint + ":5432/postgres"; |
| 49 | + |
| 50 | + return DriverManager.getConnection(url, props); |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String[] args) throws SQLException { |
| 54 | + String clusterEndpoint = System.getenv("CLUSTER_ENDPOINT"); |
| 55 | + assert clusterEndpoint != null : "CLUSTER_ENDPOINT environment variable is not set"; |
| 56 | + |
| 57 | + String clusterUser = System.getenv("CLUSTER_USER"); |
| 58 | + assert clusterUser != null : "CLUSTER_USER environment variable is not set"; |
| 59 | + |
| 60 | + String region = System.getenv("REGION"); |
| 61 | + assert region != null : "REGION environment variable is not set"; |
| 62 | + |
| 63 | + try (Connection conn = ExampleWithNoConnector.getConnection(clusterEndpoint, clusterUser, region)) { |
| 64 | + if (!clusterUser.equals("admin")) { |
| 65 | + Statement setSchema = conn.createStatement(); |
| 66 | + setSchema.execute("SET search_path=myschema"); |
| 67 | + setSchema.close(); |
| 68 | + } |
| 69 | + // Create a new table named owner |
| 70 | + Statement create = conn.createStatement(); |
| 71 | + create.executeUpdate(""" |
| 72 | + CREATE TABLE IF NOT EXISTS owner( |
| 73 | + id uuid NOT NULL DEFAULT gen_random_uuid(), |
| 74 | + name varchar(30) NOT NULL, |
| 75 | + city varchar(80) NOT NULL, |
| 76 | + telephone varchar(20) DEFAULT NULL, |
| 77 | + PRIMARY KEY (id))"""); |
| 78 | + create.close(); |
| 79 | + |
| 80 | + // Insert some data |
| 81 | + Statement insert = conn.createStatement(); |
| 82 | + insert.executeUpdate( |
| 83 | + "INSERT INTO owner (name, city, telephone) VALUES ('John Doe', 'Anytown', '555-555-1999')"); |
| 84 | + insert.close(); |
| 85 | + |
| 86 | + // Read back the data and assert they are present |
| 87 | + String selectSQL = "SELECT * FROM owner"; |
| 88 | + Statement read = conn.createStatement(); |
| 89 | + ResultSet rs = read.executeQuery(selectSQL); |
| 90 | + while (rs.next()) { |
| 91 | + assert rs.getString("id") != null; |
| 92 | + assert rs.getString("name").equals("John Doe"); |
| 93 | + assert rs.getString("city").equals("Anytown"); |
| 94 | + assert rs.getString("telephone").equals("555-555-1999"); |
| 95 | + } |
| 96 | + |
| 97 | + // Delete some data |
| 98 | + String deleteSql = String.format("DELETE FROM owner where name='John Doe'"); |
| 99 | + Statement delete = conn.createStatement(); |
| 100 | + delete.executeUpdate(deleteSql); |
| 101 | + delete.close(); |
| 102 | + } |
| 103 | + System.out.println("Connection exercised successfully"); |
| 104 | + } |
| 105 | +} |
0 commit comments