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   

    }


}

Monday, October 11, 2010

Difference between Struts and JSF

Struts is an Action framework and JSF is component framework.
Action Framework: Struts (both 1 and 2) are action frameworks. In essence they give you the ability to map URLs to activities and code on the back end. Here, the layout and workflow tends to be more page oriented. As a developer you tend to interact with the HTTP request cycle directly, though Struts 2 helps isolate at least the binding of the request data to the action implementation classes.
Action framework coders can have more control of the structure and presentation of URLs, since their systems are more intimately tied to them compared to a component framework.
Component Framework: In a component framework, artifacts that are rendered on the page are initially developed as individual components, much like in modern GUI "fat client" libraries. You have components, they have events, and your code is written to work with those events against the components. Most of the time, in mainstream development, your code is pretty much ignorant of the HTTP request cycle and processing.
JSF eliminated the need of Form Bean and DTO classes as it allows the use of same pojo class on all tiers of the application because of the Backing Bean.
In struts framework we can access the request and response objects directly, as in case of JSF we can access request and response objects indirectly.
Struts has a more sophisticated controller architecture than does JavaServer Faces technology. Struts is more sophisticated partly because the application developer can access the controller by creating an Action object that can integrate with the controller, whereas JavaServer Faces technology does not allow access to the controller.
In addition, the Struts controller can do things like access control on each Action based on user roles. This functionality is not provided by JavaServer Faces technology.
Struts frameworks is  better for "web sites", site like this one, sites that focus on delivering content to the user. Where it's mostly a "read only" experience for the end user who is likely to want to bookmark things, come back to arbitrarily deep pages, etc.
JSF framework is better for CRUD screens, back office applications, lots of forms and controls, etc. Here, the workflow is more controlled. You tend to not get to a "detail" screen with going through the "list" screen or "header" screen first, for example.
struts validate full beans (validator.xml) jsf has a pluggable validator-mechanism

Friday, October 1, 2010

Available implementations and Plugins of JSF?


JSF is the java specification given by the sun microsystems. For this specifications there are multiple;e implementations from the different third party vendors. 
The main implementations of JavaServer Faces are:
  1. Reference Implementation (RI) by Sun Micro systems.
  2. Apache MyFaces is an open source JavaServer Faces (JSF) implementation or run-time.
  3. ADF Faces is Oracle’s implementation for the JSF standard.

The main Pluginsof JavaServer Faces are: 
  1. Ice Faces FROM ICEsoft Technologies Inc.
  2. Prime Faces from Prime Technology.
  3. Rich Faces from JBoss
  4. Open Faces from TeamDevLtd
  5. DOJO Faces from DOJO 

Advantages of JSF

The major benefits of JavaServer Faces (JSF) technology are:
  1. JavaServer Faces architecture makes it easy for the developers to use. In JavaServer Faces technology, user interfaces can be created easily with its built-in UI component library, which handles most of the complexities of user interface management. 
  2. Offers a clean separation between behavior and presentation.
  3. Provides a rich architecture for managing component state, processing component data, validating user input, and handling events.
  4. Robust event handling mechanism.
  5. Events easily tied to server-side code.
  6. Render kit support for different clients
  7. Component-level control over statefulness
  8. Highly 'pluggable' - components, view handler, etc
  9. JSF also supports internationalization and accessibility
  10. Offers multiple, standardized vendor implementations

Wednesday, September 29, 2010

Oracle Database queries

To know the current user in Oracle
select user from dual;
Steps to know list of users in the Oracle database.
Connec to dba user
conn sys/password as sysdba;
execute below query to get list of users
select * from dba_users;
To select only user names:
select username from dba_users;
To know the full details about the user:
select * from dba_users where username='scott';
To get the Table space name:
select tablespace_name from dba_tablespaces;
To list the username and size of the user:
select owner,sum(bytes) from dba_segments group by owner;

Saturday, September 25, 2010

What is JSF?

JSF stands for Java Server Faces and it is an industry standard and a server side user interface component framework for building component-based user interfaces for web applications. JSF has set of pre-assembled User Interface (UI).
It is event-driven programming model. By that it means that JSF has all necessary code for event handling and component organization. Application programmers can concentrate on application logic rather sending effort on these issues. It has component model that enables third-party components to be added like AJAX.