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.
Tuesday, August 2, 2022
How to measure elapsed time of a method in Java
Friday, June 10, 2022
Oracle Functions in private network
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.
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.
Some useful links
1. JDBC connection without wallet
2. Update your Autonomous Database Instance to Allow both TLS and mTLS Authentication
3. View TNS Names and Connection Strings for an Autonomous Database Instance
Monday, July 19, 2021
How to Connect a Go program to Oracle Autonomous Database
In this blog post, we will see how to connect Oracle Autonomous Database in Go programming language. This can be divided into three parts.
Part 1) We need to download Autonomous Database client credentials. More details
In the Autonomous database OCI console, open to the service console of the DB which we need to connect. There in the Administration tab, we will find the option to download the client credentials.
Once It is downloaded, unzip the same to a directory of choice.
Part 2) To connect to the database we need the database user, password, and the connect string.
We can use the admin user, which was provided as part of the database provision, or any other database user. Here we need to form the connect string.
example: protocol://host:port/service_name?wallet_location=/my/dir&retry_count=3&retry_delay=20
We need to form our connect string for the autonomous database we want to connect. We will get details in the tnsnames.ora file which is part of the zip we had downloaded in above Part 1.
There we will find many connection services (more details about them), We can pick the one we want to use for our connection.
example:
db202107181649_medium = (description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-mumbai-1.oraclecloud.com))(connect_data=(service_name=iro8q5fzknp5ge4_db202107181649_medium.adb.oraclecloud.com))(security=(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")))
Now by referring to this we can create our connection string like below.
tcps://adb.ap-mumbai-1.oraclecloud.com:1522/iro8q5fzknp5ge4_db202107181649_medium.adb.oraclecloud.com?wallet_location=/Users/pallab/tool/wallet_DB/
The wallet_location is the path where we had unzipped our client credentials in above Part 1.
Part 3) Now let's write our go code.
We will use godror package to connect.
Install godor # go get github.com/godror/godror
We need runtime dependency for Oracle Client libraries, We can download the free basic or free light version from https://www.oracle.com/database/technologies/instant-client/downloads.html.
Note: In sql.open() we have passed our oracle client libraries which we downloaded and unzipped as libDir parameter.
Useful Links
1. https://blogs.oracle.com/opal/how-connect-to-oracle-autonomous-cloud-databases
2. https://blogs.oracle.com/developers/how-to-connect-a-go-program-to-oracle-database-using-goracle
3. https://godror.github.io/godror/doc/installation.html
Monday, August 10, 2020
Hosting a static website on Oracle Object Storage

Npm - https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
{
"webdir" : "/Users/pallab/mylab/oci-os-static-web", # This is the path of the static web need to be uploaded
"index" : "index.html", # Its the index/home page of your website
"configurationFilePath" : "~/.oci/config", # OCI credential configuration
"configProfile" : "DEFAULT", # OCI credentail config profile
"comaprtmentOCId" : "ocid1.compartment.oc1......", # OCI compartment OCID where we want to upload
"bucketName" : "myexpdemo" # bucket name to be created
}
[DEFAULT]
user=ocid1.user.oc1..<your_unique_id>
fingerprint=<your_fingerprint>
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..<your_unique_id>
customCompartmentId=ocid1.compartment.oc1..<your_unique_id>
Sunday, May 10, 2020
Run a Spring Boot application on OCI Compute Instance
Creating a new Spring Boot app and running it locally
package myexpjava.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controllerpublic class DemoController {
@GetMapping("/hello")
@ResponseBody
public String sayHello() {
return "Hello World !!!";
}
}
./gradlew bootRun
This command will run the application and we can test the same in any browser in the url
http://localhost:8080/hello/
It should print Hello World !!! in browser
Creating an OCI compute instance and deploy the app.
We will try to deploy this app directly to a single OCI compute instance and we will run it.
For this example, we have used the standard Oracle-Linux-7.8 image.
To create a compute instance in OCI console, we need to navigate compute and create instance. To access the instance we need ssh keys, to genarte the same please refer https://docs.cloud.oracle.com/en-us/iaas/Content/Compute/Tasks/managingkeypairs.htm
Once the compute is ready. We need to install Java, as our Spring Boot App requires a java environment to run.
1. ssh to compute instance
ssh -i ~/.ssh/oci_compute opc@<public ip> of the compute instance>
2. Install java in the compute instance, for this example jre is enough.
sudo yum install jre-12.x86_64
3. Configure the firewall to open port 8080, as our app will run in 8080
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
Make a directory where we will copy our spring app
mkdir spring-app/
4. Network configuration to allow Internet traffic to our compute instance.
a) We need an internet gateway configured in the VCN where the compute instance belongs
c) Security Ingress rule needs to be added to allow traffic.
5. Copy our Spring Boot app from laptop to compute instance and run.
./gradlew clean build
This will build and create the spring-web-rest-demo-1.0.jar in ./build/libs
To copy the same to our compute instance
scp -i ~/.ssh/oci_compute build/libs/spring-web-rest-demo-1.0.jar opc@<public ip>:spring-app/
Now ssh to the compute and run the app
ssh -i ~/.ssh/oci_compute opc@<public ip> of the compute instance>
java -jar spring-app/spring-web-rest-demo-1.0.jar
Our spring app should run in port 8080. If all the configs are correct
we should able to see Hello World !!! in the browser, by trying compute's
http://<public ip>:8080/hello
If you don't see please revisit step 3 and 4
| |
Wednesday, February 12, 2020
Connecting Google cloud SQL from App engine.
Tuesday, August 6, 2019
Rest apis powered by GraphQL
https://github.com/pallabrath/myexpjava/tree/master/GqlDemo
1. Lets start with build.gradle
We have the dependencies for jersey, graphql, hibernate, ojdbc driver and some helpers like gson and gauva.
2. In web.xml we have initialised the jersey servlet, there we have specified init parameters to look for com.rest package.
3. Inside source we have com.rest.Employee.java which expose the rest end point GqlDemo/rest/employee
We have the Query() method to handle the incoming http post request for graph ql queries.
4. We have created GraphQl provider which load the schema definitions from resources/schema.graphqls
we have defined the wiring for query employeeById.
5. In GraphQLDataFetcher we have the implementation for employeeById. where we get the request parameter value and process. Here I have used hibernate to query the database.
Here is a screen shot of postman request/response.
I will try to answer if any question on this. Please let me know your feedback.