Saturday, 7 September 2013

About Hibernate SessionFactory!

Fact about hibernate SessionFactory


Questions: - What is SessionFactory in hibernate?


Answer: - In Hibernate, SessionFactory is an interface, which is available in “org.hibernate” package. The delivery of session objects to hibernate applications is done by this interface. The SessionFactory object should be instantiated once during the application initialization.
  •  Session factory is long lived and thread safe object, so it can be shared in multithreaded environment.
  •  Generally only one SessionFactory is enough for an application but if application is interacting with more than one database then one SessionFactory should be created for one database.

Configuration cfg=new Configuration();   // This statement will create an empty object.
cfg=cfg.configure();


when we called  configure()  method then It looks for hibernate.cfg.xml configuration file and it also looks for Hibernate mapping(.hbm) file.

 SessionFactory sessionfactory = cfg.buildSessionFactory();
  • SessionFactory object will be created once and will be used by multiple users. It’s a single data store point and it is thread safe. Many threads can access this concurrently.
  • Session Factory object is the factory for session objects and it must be built only once at startup.

  • SessionFactory acts as a client of  “org.hibernate.connection.ConnectionProvider”.

  • SessionFactory maintains a second level cache of data that is reusable between transactions at a process or cluster level.

If we are using two databases like mysql and oracle in our hibernate application then we need to build two SessionFactory objects like below:-
Configuration configuration =new Configuration ();
Configuration cfg1= configuration.configure(“hibernate-mysql.cfg.xml”);
SessionFactory sessionfactory1=cfg1.buildSessionFactory ();

Configuration cfg2= configuration.configure(“hibernate-oracle.cfg.xml”);
SessionFactory sessionfactory2=cfg2.buildSessionFactory ();

In an application, to get the SessionFactory objects we should create a utility class and using their methods we should get the SessionFactory object. Below is the example of HibernateUtility Class.

package com.gaurav.common.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtility {
                private static final SessionFactory sessionFactory = createSessionFactory();

                private static SessionFactory createSessionFactory() {
                                try {
                                                return new Configuration().configure().buildSessionFactory();
                                } catch (Exception ex) {
                                                System.err.println("SessionFactory creation failed");
                                                throw new ExceptionInInitializerError(ex);
                                }
                }

                /**
                 * @return the sessionfactory
                 */
                public static SessionFactory getSessionfactory() {
                                return sessionFactory;
                }

}

So, if we are using the above utility then we can use below code to get the session objects.



SessionFactory sf = HibernateUtility.getSessionfactory();

                                Session session = sf.openSession();

No comments:

Post a Comment