1. Create JdbcLatencyTest.java file and copy below code.
2. Do the changes for respective DB Driver class name and JDBC string along with Username & Password.
// ###################### JdbcLatencyTest.java File Contents ############# //
**********************************************************
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JdbcLatencyTest {
private static final int NUM_ITERATIONS = 10;
public static void main(String[] args) {
// JDBC driver class name and database URL string
String driver = "oracle.jdbc.xa.client.OracleXADataSource";
String url = "jdbc:oracle:thin:@123.789.123.456/SERVICENAME";
// JDBC connection properties for Username and password
String username = "username";
String password = "Password";
// Array to store latency measurements
long[] latencyArray = new long[NUM_ITERATIONS];
for (int i = 0; i < NUM_ITERATIONS; i++) {
Connection conn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String connectionStartTime = null;
String connectionEndTime = null;
try {
// Load the JDBC driver
Class.forName(driver);
// Start the timer for connection establishment
long startConnectionTime = System.currentTimeMillis();
// Capture the timestamp when the connection is created
connectionStartTime = dateFormat.format(new Date());
// Create the connection
conn = DriverManager.getConnection(url, username, password);
// Calculate the elapsed time for connection establishment
long endConnectionTime = System.currentTimeMillis();
long connectionLatency = endConnectionTime - startConnectionTime;
// Capture the timestamp when the connection is closed
connectionEndTime = dateFormat.format(new Date());
// Store the latency value in the array
latencyArray[i] = connectionLatency;
// Print detailed latency information
System.out.println("Iteration " + (i + 1) + ":");
System.out.println(" Connection Start Time: " + connectionStartTime);
System.out.println(" Connection End Time: " + connectionEndTime);
System.out.println(" Latency: " + connectionLatency + " ms");
// Close the connection
conn.close();
} catch (ClassNotFoundException e) {
// Handle driver loading exception
e.printStackTrace();
} catch (SQLException e) {
// Handle connection exception
e.printStackTrace();
}
}
// Print the overall latency statistics
System.out.println("Connection Latency Measurements:");
for (int i = 0; i < NUM_ITERATIONS; i++) {
System.out.println("Iteration " + (i + 1) + ": " + latencyArray[i] + " ms");
}
}
}
***************************************************
FYI, JDBC Driver Class Name to connect respective DB:
- oracle.jdbc.xa.client.OracleXADataSource (for Oracle 19c and 12c DB , ojdbc8.jar with JDK8 complied)
- oracle.jdbc.pool.OracleConnectionPoolDataSource (for Oracle 11g with minimum jDK7, JDK8 complied)
- org.postgresql.Driver (for POSTGRESQL DB , with postgresql-42.6.0.jar )
- com.microsoft.sqlserver.jdbc.SQLServerXADataSource (for MSSQL DB, with mssql-jdbc-12.2.0.jre8.jar)
3. Execute the below commands for compile and run.
$JAVA_HOME/java/8.0/bin/javac -cp /home/wasadmin/ojdbc8.jar JdbcLatencyTest.java
$JAVA_HOME/java/8.0/bin/java -cp .:/home/wasadmin/ojdbc8.jar JdbcLatencyTest
################# Output #######################