Analyzing Hibernate Application

We have seen how to write a Hibernate Application, which inserts the values into DataBase Server. Now let us know what each step is doing in the background. 


Step1: When we create the Configuration object a plain java object will be created. This configuration object stores all the configuration details. Initially when we create the object no data is available in that.


Step2: When we call cfg.configure() it searches for hibernate.cfg.xml file(Configuration file) in the WEB-INF folder. If the Configuration file is not found it throws an exception "org.hibernate.HibernateException/hibernate.cfg.xml not found".
  • If the configuration file is found, parser will validate the xml file. If the file is not valid parser throws an exception "could not parse the xml file". If the xml file(configuration file) is valid  configure() method gets the values of Driverclass, url, username, password, and the information about Hibernate Mapping Files. Then it reads the contents from Hibernate mapping files and stores the entire data into Configuration Object(JVM's Memory).
Step3: When we call cfg.buildSessionFactory() it gets the driverclass, url, username, and password from the configuration object and supply as the input to the internal connection pool of Hibernate(hibernate internally uses c3p connection pool).
  • Now the connection pool program acquires connections from DBServer. Now, buildSessionFactory() picks the connection from the connection pool and establish the connection with DBServer. 
  • Now it will check weather the required tables are available or not. If the tables are not available buildSessionFactory() creates the table.
  • It is the responsibility of the buildSessionFactory() to create and process all the query operations(CURD operations) for each table. After processing the buildSessionFactory() returns the connection to the connection pool.
  • As the buildSessionFactory method is doing more work, it is recommended to call this method only once in the life cycle of the project.
Note: Repeatedly calling the configure() and buildSessionFactory() methods decreases the performance. So, it is recommended to call them only once in the project life cycle.

Step4: When we open the session object by using sf.openSession().
  1. Hibernate creates an object which is similar to hashtable object. We call this object as First Level Cache.
  2. Hibernate associates this object with Session Object.
  3. This First level cache is available until the Session object is closed. Only the session object can access the data from First Level Cache.
  • Opening the session object is nothing but getting the connection object. This method is not an expensive operation. It is always recommended to open a new session object whenever it is required. 
Step5: Whenever we create or ask the hibernate to start the transaction, internally it uses JTA (Java Transaction API) to start and end transactions. To perform Insert, update, delete operations we must start transaction, whereas to perform retrieval operation it is optional.

Step6: Create POJO class object corresponding to the table. When the object is created all the instance variables are initialized with default values.
POJO Class object in which values are initialized with null values
Fig: POJO Class object in which values are initialized with null values
Step7: Storing the data into POJO class object by calling setter methods.
variables initialized with values
Fig: variables initialized with values
Step8: When we call save() method internally hibernate take the object and add the object to First level cache, by using a key TBS(To Be Saved). The method like delete uses a key TBD(To Be Deleted). To check weather the object is available in the First Level Cache, we use a method contains().

  • Ex: hsession.contains(e);

Step9: When we call tx.commit(), hibernate checks for any objects in the First Level Cache which are waiting for an operation to be taken care. If the objects are available hibernate finds, to which POJO class these objects belongs to(Hibernate internally uses getClass() to find the class). 

  • Now hibernate finds to which table this POJO class is mapped, and from that table it gets the registration code. Based on this code it has got the query form the JVM's memory. Now hibernate get the data from the POJO class and supply as the input to the query. This query is added to batch object and send to the DBServer.
  • By default hibernate will not display the query sent to the DB. If we want to see the query we have to add a property in the hibernate configuration file. The following is the property to show SQL queries.

<property name="show-sql">true</property> 

Step10: When we call hsession.close(), the connection is returned to the connection pool as well as it removes the First Level Cache object.
Step11: When we call sf.close(), it removes second level cache as well as returns all the connections from the connection pool to DBServer.


                    

2 comments:

  1. Very good and clear explanation... thank you...

    ReplyDelete
  2. It is very accurate sir,,, nice work...

    ReplyDelete