Saturday, November 27, 2010

Caching in hibernate

Caching is a Temporary memory or buffer which resides at client side and stores the results sent by server. When client generates same request for multiple number of times, the first request generated results will be stored in cache and this result will be used across the multiple requests. This reduces the round trips between the client and server. Since the result will be collected from cache that is available at client side.

Hibernate supports for 2 levels of cache:
  1. Level one cache
  2. Level two cache.
Level One Cache:   
Level 1 cache is inbuilt cache and it will be associated with hibernate session objects. Every session object of hibernate application contains one inbuilt level one cache.

Responsibilities of Level one cache:
a)      If select query executed for multiple no of times with in a session. Only one time query goes to database software gets the result, remaining all the times result will be gathered from cache.
b)      If one of POJO class object is modified for multiple no of times with in a transaction of session object, instead of sending update query for multiple number of times, all the changes done on the object will be kept tracked and only one update query will be generated reflecting all the changes at the end of the transaction.

The different ways to remove the level 1 cache from session
i)                    Session.flush()  --> Flushes level one cache content to db software
ii)                   Session.evict()  --> Remove the content of level 1 cache
iii)                 Session.close() --> closes level 1 cache, before that it calls session.flush()
A hibernate client application can have multiple level1 caches because a hibernate application can have multiple hibernate session objects.
The data stored in level1 cache, level2 cache will be in full synchronization with table rows.

Level-2 Cache:

It is a configurable cache (Not a built in cache). Third party vendors are supplying supporting jar files for level 2 cache. Level2 cache is global cache and it is visible for all the session objects of the hibernate application.
When level-2 cache is enabled the results gathered for database software will be stored in both level 1 and level 2 caches.
sessionFactory.close()  --> Destroys the session factory object and releases level 2 cache.
sessionFactory.evict(arga …)  --> Removes pojo class object from session factory.
sessionFactory.evictQueries(args…)  --> Cleans queries related data from cache. 

If hibernate use same request as second request or different session objects then software tries to collects the results either from leve1/level2 caches.
There are different third party providers for level 2 cache:
  1. Swarm Cache
  2. OS Cache,
  3. EH Cache
  4. JBoss Tree Cache … etc.

Friday, November 19, 2010

My Hero : Stephen Hawking

Heroes are the people who can overcome fear, or help others overcome fear. 
Here I am happy to tell about is, someone who just lives his life as he is. He has faced terrible problems and has become a great person of new scientific era.
So he is my Role Model, He is my Hero, He is my Inspiration….
He is Stephen Hawking… He is a unique person with extraordinary mind.
My Hero is a person whose greatness sneaked up on the world. He was born on 8th January 1942 in England. Although there were thousands of people born on that day, but he was special. As child he is little clumsy and bit too smart.
With his smartness he became great intellect but with his clumsiness he got the disease called “Motor Neuron Disease”. With this disease he has been confined to a wheel chair from the last 4 decades and most of his external body organs stopped working. From the last 25 years he is not able to speak.


Hacking’s external organs eyes, ears and little left hand small fingers only active. In spite of all these physical disabilities he is totally dedicated to his work. 
Actually Hawking has been working on basic laws of the Universe. Hawking has done many great things, I found fallowing are the major things according to me.
He has done the extreme work in new scientific era. He has been proved that the Black Holes emit the Radiation and now it’s named as Hacking’s radiation.
In 1988 he wrote a book called “A Brief History of Time” which became the international best seller of the year.
American news paper Chicago Times wrote “Hawking is a legend right up there with Galilee and Newton and Einstein”.
Here I will share one important thing about Hawking which happened in his life. Once he was in middle of writing a book. With his disease he lost his voice. There is no other way to write or communicate except by blinking of eyes. Fortunately, he loved his work so much that he continued on with it despite the ever clinking fear dying.
After several years he has received a present from his college friend. The present was a revolutionary new Computer that attached to his Wheel chair and would talk to him. He now had a way to write his papers and lectures to students. It seemed that his life was now seems better.
I hope that, generally our problems are not bigger than Hawking’s problems.
He has not saved someone’s life, but he inspired mine. So in my opinion, He is my Hero and if you don’t mind he will always be my hero.
Are our problems are bigger than Hawking’s problems? Never right…..
So, He is My Role model, He is my inspiration finally he is my HERO…

Thanks….

Wednesday, November 3, 2010

Connection Sql Server using Java

package com.javatutorials;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SqlServerConnection {
    public static Connection connection = null;
    public static Connection getDbConnection() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager
                    .getConnection("jdbc:odbc:mydsn;user=sa;password=sa12$");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void closeConnection() {
        if (connection != null)
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }
}
mydsn in the above class is the dsn created to connect to sql server using jdbc-odbc connection.

The dsn  creating process can be found at : Create SQL Server dsn


Create Folder in Java Example

import java.io.File;
/**
 * @author mallikarjung
 *
 */
public class FolderCreation {
    public static String folderPath = "D:/SampleFolder/";
    /**
     * @param args
     */
    public static void main(String[] args) {
        File folder = new File(folderPath);
        //check whether folder exists or not
        if(!folder.exists()){
            folder.mkdir();
            System.out.println("Folder has been created Successfully ... ");
        }
    }
}

Replace String Example in Java

/**
 * @author Mallikarjun G
 *
 */
public class JavaStringReplaceExample {
    /**
     * @param args
     */
    public static void main(String args[]) {      
        String str = "Replace String";
        System.out.println(str.replace('R', 'A'));// Replaceing R with a in str
        System.out.println(str.replaceFirst("Re", "Ra"));// Replaceing First Re with Ra in str  
        System.out.println(str.replaceAll("Re", "Ra")); // Replaceing All Re with Ra in str   

    }


}