## Tuesday, August 2, 2022

### How to measure elapsed time of a method in Java

There are times we need to compute how much time is elapsed while executing a certain task. To compute elapsed time we can think something like below.

public class ElapsedTimeDemo {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
doSomething();
long endTime = System.currentTimeMillis();
System.out.println("Elapsed time = "+ (endTime - startTime));
}
public void doSomething() {
}
}

This code keeps track of start time of function execution and end time once the execution is complete. The difference between start time and end time gives us the elapsed time. This looks good. The only problem is that for start time and end time we are depending upon the system clock. Because of the clock skew system clock tends to drift away from the actual time. To make the system clock sync usual practice is computer connects to a time server and adjusts its own clock. While adjusting the system clock it may be set forward or backward based on the drift. This can cause some issues in the above program. If the clock is getting adjusted while the method execution of doSomething(), we will get the end time which can be either before or after the real end time. This makes the elapsed time calculation faulty.
To make the correct elapsed time calculation consider the below program.

public class ElapsedTimeDemo {
public static void main(String[] args) {
long startTime = System.nanoTime();
doSomething();
long endTime = System.nanoTime();
System.out.println("Elapsed time = "+ (endTime - startTime));
}
public void doSomething() {
}
}

Here instead of currentTimeMillis() we are using nanoTime(), Nano time comes from a JVM monotonic clock. A monotonic clock means it starts with a fixed origin, let's assume it's a counter that starts from 0 when JVM starts it keeps on incrementing. At any point in time counter value will not make much sense in absolute terms. In elapsed time calculation this is useful. And this doesn't get adjusted like a system clock.

References

## Friday, June 10, 2022

### Oracle Functions in private network

OCI Functions is a server-less platform. In this blog post, we will see how to run oracle functions in a private network. While creating the Application we need to select the desired VCN and private subnet.
Then these are the few things we need to configure for the subnet so that function can run.

1) Service Gateway to reach out OCI service
The function application in the private network needs to connect to the container registry and download the required image. To achieve this we need a Service gateway in the VCN. In the console Network ->  Virtual Cloud Network page we can edit the VCN to add a service gateway.

2) Route Rule for service gateway.

In the private subnet where the application is running, there would be an attached route table, and in that table, we need to add a route rule saying the OCI service calls need to be routed through the service gateway we had created in the previous step.

3) Secure Egress Rule

In that particular subnet we need to allow traffic from the subnet to the OCI service, to do so we will add a stateful Egress Rule in the security list of the subnet

After these steps function should be able to reach out desired OCI service and run.

References:

## Wednesday, January 12, 2022

### How to connect Java a program to Oracle Autonomous Database over TLS without wallet

In this blog post, we will discuss how to connect Java a program using JDBC thin driver to Oracle Autonomous Database over TLS without a wallet.

With TLS support we can now connect to ADB without the credential wallet.

Part 1: We need to configure ADB for TLS to get the TLS connection string.

a) For the ADB we want to connect over TLS, In the Autonomous database details page, we need to set Mutual TLS authentication (mTLS) as not required.

b) Then from the database connection page (we can navigate to it by clicking on DB connection button in the ADB console) chose "TLS" as TLS authentication and copy the connection string for desired TNS name. In this example, I had copied demodb_medium

Part 2: Java program which uses the above connect string in jdbc to execute sql statements.

Prereq : `ojdbc8.jar` and `ucp.jar`

`I have used JDK 11 in this eample`

`package demo;import java.sql.*;import java.util.Properties;public class ADBSharedTLSConnect {    private static String atps_tls = "(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.ap-mumbai-1.oraclecloud.com))(connect_data=(service_name=rks9000p5ge4_demodb_medium.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)(ssl_server_cert_dn=\"CN=adb.ap-mumbai-1.oraclecloud.com, OU=Oracle ADB INDIA, O=Oracle Corporation, L=Redwood City, ST=California, C=US\")))";    private static String db_url = "jdbc:oracle:thin:@" + atps_tls;    private static String dbUser = "admin";    private static String dbPwd = "test@ATP122245";    public static void main(String[] args) {        System.out.println("Connecting to ATPS over TLS...");        ResultSet rs = null;        Statement stmt = null;        Connection con = null;        try {            Class.forName("oracle.jdbc.driver.OracleDriver");            Properties props = new Properties();            props.setProperty("user", dbUser);            props.setProperty("password", dbPwd);            props.setProperty("oracle.jdbc.fanEnabled", "false");            con = DriverManager.getConnection(db_url, props);            stmt = con.createStatement();            rs = stmt.executeQuery("select sysdate from dual");            while (rs.next()) {                System.out.println(rs.getString(1));            }            System.out.println("Demo Over...");        } catch (Exception e) {            System.out.println(e);        } finally {            try {                if (rs != null) {                    rs.close();                }                if (stmt != null) {                    stmt.close();                }                if (con != null) {                    con.close();                }            } catch (SQLException e) {                e.printStackTrace();            }        }    }}`

atps_tls is the one that I had copied from part 1 step b.

I have used oracle.jdbc.fanEnabled property as false, without this configuration there was an error

SEVERE: attempt to configure ONS in FanManager failed with oracle.ons.NoServersAvailable: Subscription time out

Although query was getting executed.