SessionFactory and Session in hibernate

In this tutorial, we will see What is SessionFactory and Session in hibernate? How we will get Session and SessionFactory object? Also, we will see Can we create multiple SessionFactory in one application?


SessionFactory and Session in hibernate


SessionFactory In Hibernate.

SessionFactory is an interface available in org.hibernate package which extends Referenceable and Serializable interface and provides factory methods to get session object. Let’s see some points about SessionFactory.

  • SessionFactory is thread safe so multiple threads can access the SessionFactory at the same time.
  • SessionFactory is Immutable. Once we create SessionFactory we can not modify it(If you look SessionFactory methods we don’t have any setter kind of method)
  • SessionFactory is created at the time of application startup, it reads all information from the configuration file(hibernate.cfg.xml file). We will see later in details.
  • We should have one SessionFactory for one database configuration.
  • We can create a SessionFactory object using the Configuration class as below.

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Let’s see what happens when the above line would execute.

Here first we are creating a Configuration class object and call configure() method, which has two overloaded versions. One without parameter like configure() and another one is configure(String resources). If we use configure() by default it looks for hibernate.cfg.xml file, because internally inside configure() method hibernate.cfg.xml is defined. If we use configure("hibernate.cfg.xml file") method then another overloaded version will get called. If we change our cfg file name something like then we need to call other overloaded version i.e configure("") method.

Let’s see how configure() method has been implemented in hibernate.

Observe the above code snippet. Since inside configure() method another overloaded version is getting called which loads hibernate.cfg.xml file. The only thing we need to make sure hibernate.cfg.xml file should be in the correct path.

SessionFactory and Session in hibernate

This sessionFactory object will have all the information whatever we have provided in hibernate.cfg.xml file.


Example to get SessionFactory object – 




Session In Hibernate.

Session is an interface available in org.hibernate package which provides different API to communicate java application to hibernate. Let’s see some points related to Session.

  • The session is not thread-safe.
  • The main use of the Session object to perform create, get, and delete operations for java classes(entity).
  • The first level cache belongs to the session object. See an example of the first-level cache in detail.
  • We can have multiple sessions for a SessionFactory.
  • We can get Session object using SessionFactory reference as below.

Session session = sessionFactory.openSession();


Example to get Session object –


Can we create multiple SessionFactory in one application?

Yes, we can. But we should have two databases. There should be one SessionFactory per database. Suppose we are using two databases and we have two hibernate.cfg.xml files which contain two database configuration details.

Let’s say we have two configuration files hibernate1.cfg.xml and hibernate2.cfg.xml. The hibernate1.cfg.xml contains oracle database related configuration and hibernate2.cfg.xml contain MySQL database related configuration something like below.





Note – Both Configuration files(hibernate1.cfg.xml and hibernate2.cfg.xml) has been defined in same application.

We can get two SessionFactory  Using  Configuration object as below.

SessionFactory sessionFactory1 = new Configuration(“hibernate1.cfg.xml“).configure().buildSessionFactory();

SessionFactory sessionFactory2 = new Configuration(“hibernate2.cfg.xml“).configure().buildSessionFactory();

what is happening in above code? We are creating Configuration class object and calling configure() method. This configure() method loads all database details(from configuration file) and further it will return SessionFactory.

So one Database one SessionFactory another database another SessionFactory.

Now the question comes what will happen if we will try to create multiple Sessionfactory using same database configuration something like below.

SessionFactory sessionFactory1 = new Configuration(“hibernate1.cfg.xml“).configure().buildSessionFactory();

SessionFactory sessionFactory2 = new Configuration(“hibernate1.cfg.xml“).configure().buildSessionFactory();

SessionFactory sessionFactory3 = new Configuration(“hibernate1.cfg.xml“).configure().buildSessionFactory();


Did you notice we are using same database configuration file(i.e hibernate1.cfg.xml) for all three SessionFactory?

This will work fine. No exception will come. But it doesn’t make any sense. right? SessionFactory is something Util kind of thing. Create once and use throughout the application. Similar to the static method. Creating multiple SessionFactory for a single database going to impact performance, Since SessionFactory object contains the connection information, hibernate configuration information and other information.

Hope this makes sense.

Summary : –

We can create a SessionFactory object using the Configuration class object. Once we will have a SessionFactory object then we can have Session object. We can have multiple SessionFactory for one application.

That’s all about SessionFactory and Session in hibernate.

You may like.

SessionFcatory Docs.

Session Docs.