Skip to content

Commit 6fe29b3

Browse files
nnmehtaQuChen88
authored andcommitted
Add Multi-threaded concurrent environment to DBConnectionWithCacheExample program
1 parent 90e3d05 commit 6fe29b3

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

examples/AWSDriverExample/src/main/java/software/amazon/DatabaseConnectionWithCacheExample.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import software.amazon.util.EnvLoader;
44
import java.sql.*;
55
import java.util.*;
6+
import java.util.logging.Logger;
67

78
public class DatabaseConnectionWithCacheExample {
89

@@ -14,9 +15,12 @@ public class DatabaseConnectionWithCacheExample {
1415
private static final String USERNAME = env.get("DB_USERNAME");
1516
private static final String PASSWORD = env.get("DB_PASSWORD");
1617
private static final String USE_SSL = env.get("USE_SSL");
18+
private static final int THREAD_COUNT = 8; //Use 8 Threads
19+
private static final long TEST_DURATION_MS = 16000; //Test duration for 16 seconds
1720

1821
public static void main(String[] args) throws SQLException {
1922
final Properties properties = new Properties();
23+
final Logger LOGGER = Logger.getLogger(DatabaseConnectionWithCacheExample.class.getName());
2024

2125
// Configuring connection properties for the underlying JDBC driver.
2226
properties.setProperty("user", USERNAME);
@@ -28,20 +32,35 @@ public static void main(String[] args) throws SQLException {
2832
properties.setProperty("cacheEndpointAddrRo", CACHE_RO_SERVER_ADDR);
2933
properties.setProperty("cacheUseSSL", USE_SSL); // "true" or "false"
3034
properties.setProperty("wrapperLogUnclosedConnections", "true");
31-
String queryStr = "select * from cinemas";
32-
String queryStr2 = "SELECT * from cinemas";
35+
String queryStr = "/* cacheTTL=300s */ select * from cinemas";
3336

34-
for (int i = 0 ; i < 5; i++) {
35-
// Create a new database connection and issue queries to it
37+
// Create threads for concurrent connection testing
38+
Thread[] threads = new Thread[THREAD_COUNT];
39+
for (int t = 0; t < THREAD_COUNT; t++) {
40+
// Each thread uses a single connection for multiple queries
41+
threads[t] = new Thread(() -> {
42+
try {
43+
try (Connection conn = DriverManager.getConnection(DB_CONNECTION_STRING, properties)) {
44+
long endTime = System.currentTimeMillis() + TEST_DURATION_MS;
45+
try (Statement stmt = conn.createStatement()) {
46+
while (System.currentTimeMillis() < endTime) {
47+
ResultSet rs = stmt.executeQuery(queryStr);
48+
System.out.println("Executed the SQL query with result sets: " + rs.toString());
49+
}
50+
}
51+
}
52+
} catch (Exception e) {
53+
LOGGER.warning("Error: " + e.getMessage());
54+
}
55+
});
56+
threads[t].start();
57+
}
58+
// Wait for all threads to complete
59+
for (Thread thread : threads) {
3660
try {
37-
Connection conn = DriverManager.getConnection(DB_CONNECTION_STRING, properties);
38-
Statement stmt = conn.createStatement();
39-
ResultSet rs = stmt.executeQuery(queryStr);
40-
ResultSet rs2 = stmt.executeQuery(queryStr2);
41-
System.out.println("Executed the SQL query with result sets: " + rs.toString() + " and " + rs2.toString());
42-
Thread.sleep(2000);
61+
thread.join();
4362
} catch (InterruptedException e) {
44-
throw new RuntimeException(e);
63+
LOGGER.warning("Thread interrupted: " + e.getMessage());
4564
}
4665
}
4766
}

0 commit comments

Comments
 (0)