33import software .amazon .util .EnvLoader ;
44import java .sql .*;
55import java .util .*;
6+ import java .util .logging .Logger ;
67
78public 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