|
14 | 14 | import java.sql.SQLException; |
15 | 15 | import java.sql.Statement; |
16 | 16 |
|
| 17 | +import software.amazon.dsql.jdbc.OCCTransactionRunner; |
| 18 | + |
17 | 19 | /** |
18 | 20 | * Aurora DSQL example using HikariCP with Aurora DSQL JDBC Connector |
19 | 21 | * <p> |
|
30 | 32 | public class ExamplePreferred { |
31 | 33 |
|
32 | 34 | private final HikariDataSource dataSource; |
| 35 | + private final OCCTransactionRunner transactionRunner; |
33 | 36 |
|
34 | 37 | public ExamplePreferred(String endpoint, String user) { |
35 | 38 | this.dataSource = initializeConnectionPool(endpoint, user); |
| 39 | + this.transactionRunner = OCCTransactionRunner.create(dataSource); |
36 | 40 | } |
37 | 41 |
|
38 | 42 | private HikariDataSource initializeConnectionPool(String endpoint, String username) { |
@@ -82,30 +86,35 @@ public Connection getConnection() throws SQLException { |
82 | 86 | return this.dataSource.getConnection(); |
83 | 87 | } |
84 | 88 |
|
85 | | - private void executeExample(Connection conn, int connectionNumber) throws SQLException { |
| 89 | + private void executeExample(int connectionNumber) throws SQLException { |
86 | 90 | // Create a new table named owner |
87 | | - try (Statement create = conn.createStatement()) { |
88 | | - create.executeUpdate(""" |
89 | | - CREATE TABLE IF NOT EXISTS owner( |
90 | | - id uuid NOT NULL DEFAULT gen_random_uuid(), |
91 | | - name varchar(30) NOT NULL, |
92 | | - city varchar(80) NOT NULL, |
93 | | - telephone varchar(20) DEFAULT NULL, |
94 | | - PRIMARY KEY (id))"""); |
95 | | - } |
| 91 | + transactionRunner.runVoid(conn -> { |
| 92 | + try (Statement create = conn.createStatement()) { |
| 93 | + create.executeUpdate(""" |
| 94 | + CREATE TABLE IF NOT EXISTS owner( |
| 95 | + id uuid NOT NULL DEFAULT gen_random_uuid(), |
| 96 | + name varchar(30) NOT NULL, |
| 97 | + city varchar(80) NOT NULL, |
| 98 | + telephone varchar(20) DEFAULT NULL, |
| 99 | + PRIMARY KEY (id))"""); |
| 100 | + } |
| 101 | + }); |
96 | 102 |
|
97 | 103 | // Insert some data with a unique identifier |
98 | 104 | String uniqueName = "John Doe " + System.currentTimeMillis() + "_" + connectionNumber; |
99 | | - try (PreparedStatement insert = conn.prepareStatement( |
100 | | - "INSERT INTO owner (name, city, telephone) VALUES (?, ?, ?)")) { |
101 | | - insert.setString(1, uniqueName); |
102 | | - insert.setString(2, "Anytown"); |
103 | | - insert.setString(3, "555-555-1991"); |
104 | | - insert.executeUpdate(); |
105 | | - } |
| 105 | + transactionRunner.runVoid(conn -> { |
| 106 | + try (PreparedStatement insert = conn.prepareStatement( |
| 107 | + "INSERT INTO owner (name, city, telephone) VALUES (?, ?, ?)")) { |
| 108 | + insert.setString(1, uniqueName); |
| 109 | + insert.setString(2, "Anytown"); |
| 110 | + insert.setString(3, "555-555-1991"); |
| 111 | + insert.executeUpdate(); |
| 112 | + } |
| 113 | + }); |
106 | 114 |
|
107 | 115 | // Read back the data and verify |
108 | | - try (PreparedStatement read = conn.prepareStatement("SELECT * FROM owner WHERE name = ?")) { |
| 116 | + try (Connection conn = dataSource.getConnection(); |
| 117 | + PreparedStatement read = conn.prepareStatement("SELECT * FROM owner WHERE name = ?")) { |
109 | 118 | read.setString(1, uniqueName); |
110 | 119 | try (ResultSet rs = read.executeQuery()) { |
111 | 120 | while (rs.next()) { |
@@ -136,30 +145,26 @@ public static void main(String[] args) throws SQLException { |
136 | 145 |
|
137 | 146 | try { |
138 | 147 |
|
139 | | - // Demonstrate connection pooling with multiple concurrent connections |
140 | | - System.out.println("Testing connection pool with multiple connections..."); |
| 148 | + // Demonstrate connection pooling with OCC retry |
| 149 | + System.out.println("Testing connection pool with OCC retry..."); |
141 | 150 |
|
142 | | - try (Connection conn1 = example.getConnection(); |
143 | | - Connection conn2 = example.getConnection(); |
144 | | - Connection conn3 = example.getConnection()) { |
| 151 | + System.out.println("Connection 1 obtained from pool"); |
| 152 | + example.executeExample(1); |
145 | 153 |
|
146 | | - System.out.println("Connection 1 obtained from pool"); |
147 | | - example.executeExample(conn1, 1); |
| 154 | + System.out.println("Connection 2 obtained from pool"); |
| 155 | + example.executeExample(2); |
148 | 156 |
|
149 | | - System.out.println("Connection 2 obtained from pool"); |
150 | | - example.executeExample(conn2, 2); |
| 157 | + System.out.println("Connection 3 obtained from pool"); |
| 158 | + example.executeExample(3); |
151 | 159 |
|
152 | | - System.out.println("Connection 3 obtained from pool"); |
153 | | - example.executeExample(conn3, 3); |
154 | | - } |
155 | | - |
156 | | - try (Connection conn = example.getConnection(); |
157 | | - PreparedStatement cleanup = conn.prepareStatement("DELETE FROM owner WHERE name LIKE ?")) { |
158 | | - cleanup.setString(1, "%John Doe%"); |
159 | | - int deletedRows = cleanup.executeUpdate(); |
160 | | - |
161 | | - System.out.println("Cleaned up " + deletedRows + " test records"); |
162 | | - } |
| 160 | + // Cleanup |
| 161 | + example.transactionRunner.runVoid(conn -> { |
| 162 | + try (PreparedStatement cleanup = conn.prepareStatement("DELETE FROM owner WHERE name LIKE ?")) { |
| 163 | + cleanup.setString(1, "%John Doe%"); |
| 164 | + int deletedRows = cleanup.executeUpdate(); |
| 165 | + System.out.println("Cleaned up " + deletedRows + " test records"); |
| 166 | + } |
| 167 | + }); |
163 | 168 |
|
164 | 169 | } finally { |
165 | 170 | // Graceful shutdown |
|
0 commit comments