Find and delete blocking sessions- Oracle

Following query would give you all the session running in your oracle database with query details

select sesion.sid,
from v$sqlarea sqlarea, v$session sesion
where sesion.sql_hash_value = sqlarea.hash_value
and sesion.sql_address = sqlarea.address
and sesion.username is not null;

Now you might want to kill an unwanted session


Double Check Locking- pattern

Double check locking pattern is used for double checking the condition before acquiring a lock on an object. This helps reducing unneccary locks.

To understand lets take a simple example of creating a singleton object.

public static MySingleton getInstance()
mysingleton=new MySingleton();
return mysingleton;

The challenge with above code is that it is not threadsafe. So we might end up creating multiple objects of MySingleton in case of multithreaded environment. So we will add synchronization.

public static synchronized MySingleton getInstance()
mysingleton=new MySingleton();
return mysingleton;

Though we have saved our code from multi threading, we have added additional load of synchronization, i.e. each thread will have to wait for previous thread to finish processing of this method before entering into it. The actual error case will occur only for first time, i.e. when mysingleton object is null, we would like to stop other threads to execute the block. So to optimiza the code, we will use a technique called double locking.

public static synchronized MySingleton getInstance()
synchronized(MySingleton.class) {
mysingleton=new MySingleton();
return mysingleton;

You can see we are applying synchronized block only when we are sure that we are in a case we want to stop multiple threads from going ahead, rather than blocking threads all the time.

Setting File permission in Linux

There are three type of groups who can access a file in linux.

User: who created the file
Group: Group to which user belongs
Others: Other users (not belonging to above 2 categories).

To add/ modify permissions use following format

chmod u=rwx,g=rw,o=rx abc.txt

or a simpler option

chmod 765 abc.txt

Both of the above means same.

How to interpret these numbers


hence Read+Write+Exceite=4+2+1=7 or Read+ execute=4+1=5 and so on.

Longest repeating sub-sequence

Finding longest repeating sub-sequence or substring among a string is classic CS problem. Here is one simple solution where we use approach of finding all the substrings from beginning and sort the resulting array. In sorted array we will find longest common repeating sub-sequence.

Input: banana

Output: ana

//method to return longest subsequence
public static String findLongestSubsequence(String str)
String sub=””;

String arr[]=new String[str.length()];
//Firstly lets find all the substrings
for(int i=0;i<str.length();i++)

//as we have the list of all substrings, sort them

//compare strings with next string and find the maximum length common substring
for(int i=0;i<arr.length-1;i++)
//compare arr[i], arr[i+1]
int len=(arr[i].length()<arr[i+1].length())?arr[i].length():arr[i+1].length();
String temp=””;

for(int j=0;j<len;j++) { //compare two strings till a point where they are equal if(arr[i].charAt(j)==arr[i+1].charAt(j)) temp=arr[i].substring(0,j+1); else break; } sub=(temp.length()>sub.length())?temp:sub;

return sub;

There are other ways to find longest repeating sub-sequence. One  important way is to create a m*n matrix with a value showing length of subsequence till that point

b a n a n a
b   0 0 0 0 0 0
a   0 0 0 1 1 1
n   0 0 0 1 2 2
a   0 1 1 1 2 3
n   0 1 2 2 2 3
a   0 1 2 3 3 3

Another sophisticated way is to create a suffix tree. Read more here -


Checked vs Unchecked exceptions

Exceptions in Java can be broadly categorized into checked and unchecked exceptions. A checked exception is one for which compiler will force you to handle it (either catch it, or throw it back), whereas for an unchecked exception you are not forced to provide handler. So when you are creating a new exception, you can decide whether it is a checked (extending a checked exception) or unchecked exception (extending unchecked exception).

Now the challenge is, how to decide if an excpetion should be checked or unchecked. That is tricky.

“Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.”

And to sum it up

“Here’s the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.”

Text Source:

Cron to run every 30 minutes

How will you set up a job to run every 30 minutes, from midnight till 10 in morning, Monday to Friday?

*/30 0-10 * * 1-5 MyJob

Understanding the syntax

* * * * * *
| | | | | |
| | | | | +– Year (range: 1900-3000)
| | | | +—- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +—— Month of the Year (range: 1-12)
| | +——– Day of the Month (range: 1-31)
| +———- Hour (range: 0-23)
+———— Minute (range: 0-59)

More on Cron Jobs

Split a String in controlled manner

You must have used String’s split method a lot of time to split based on a regex or a simple character or sub-string. But did you know that you can apply the split function in controlled manner. i.e. you can specify how many time split should occur or number of max sub-strings you can expect.

Example if I have string boo:and:foo and I will control number of splits to 2 like str.split(“:”,2), I will get 2 substrings, “boo” and “and:foo”.

Read more –,%20int%29

JPA- Java Persistence API

From the very beginning of software development and more so with web applications, the need for storing state and details of objects have been there. The most used manner to store information has been databases. And with storing data into database, need for a standardized way to store this data has been felt.

The most common way to persist data into database is JDBC, i.e. Java Database connectivity. JDBC is Java Specific implementation of ODBC (Open Database Connectivity). It allows simple way for Java to execute SQL commands and interact with Database. Though it is easy to use, JDBC has a major challenge of SQL portability across Database vendors. The SQL command which works fine for oracle might now work for mysql or ms-sql.

Another popular approach for data persistence is ORM or object relational mapping. The idea is to map Java objects (POJOs) directly to a table in database. There are many strong players like Toplink (now eclipse link) and hibernate in this space.

Then came another important player EJB- entity beans. With EJB 2.0, support for persistence was good, but at cost of implementing complex EJBs.

There is another specification for persistence, JDO, Java Data Objects, which is mostly used to save Objects directly and hence make a good case for non relational or no-sql database usage.

Because of all the above mentioned options available for persistence in Java, need for standards became very important, which resulted first in JPA 1.0 and later a more complete specification in form of JPA 2.0.