25

Why use only one SessionFactory Object per application? What are the advantages of using single session factory object per application?

Viraj Dhamal
  • 4,981
  • 10
  • 29
  • 40

4 Answers4

42

Session factory objects are to be implemented using the singleton design pattern. Instances of SessionFactory are thread-safe and typically shared throughout an application. As these objects are heavy weight because they contains the connection information, hibernate configuration information and mapping files,location path. So creating number of instances will make our application heavy weight. But the session objects are not thread safe. So in short it is - SessionFactory objects are one per application and Session objects are one per client.

Hence it would be one SessionFactory per DataSource. Your application may have more than one DataSource so you may have more than one SessionFactory in that instance. But you would not want to create a SessionFactory more than once in an application.

Advantages: Obviously its improving performance of your application :)

UPDATE - Extract from Hibernate Doc

The internal state of a SessionFactory is immutable. Once it is created this internal state is set. This internal state includes all of the metadata about Object/Relational Mapping.

DarkHorse
  • 2,672
  • 17
  • 28
  • 2
    No you are wrong DarkHorse SessionFactory is not implemented using singleton design pattern. – Deepak Agrawal Mar 31 '14 at 18:23
  • 3
    @DarkHorse SessionFactory is not Singleton , it is used as singleton , it is immutable http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/SessionFactory.html – anshulkatta Jul 03 '14 at 09:11
  • An answer down here says that memory problems come from creating a SessionFactory for every request. Yet your answer seems to imply it's not possible, since SessionFactory is already a singleton. – parsecer Dec 18 '19 at 11:46
16

Because creation of a SessionFactory is an extremely expensive process which involves parsing hibernate configuration/mapping properties and creating database connection pool .Creating a database connection pool requires establishing database connections (i.e creating Connection objects) which has overhead due to the time taken to locate the DB server , establish a communication channel and exchange information to do authentication.

So if you create a SessionFactory for every request , it implies that you are not using database connection pool to serve your request .You have to setup a new connection by the above overheaded process for every request instead of just getting the opened connection from the database connection pool.

Ken Chan
  • 64,456
  • 22
  • 117
  • 138
3

There are some key points regarding one SessionFactory Object per application : -

1.Single Data Store : – It is single data store for your whole application. Although you can have multiple SessionFactory but each SessionFactory will have one different database associated with it.

2.Thread Safe : – SessionFactory is thread safe so due to this feature many thread can access the SessionFactory.

3.Immutable : - Once the SessionFactory's object is created you can not change or set the values of Session Facotyr. Its internal state is set at the time of creation.

4.Singleton : – SessionFactory is built at the time of application startup and it follows the singleton design pattern.

I hope this would answer your question..

For more details on how to create sessionfactory please have refer to URL : http://techpost360.blogspot.in/2015/07/what-is-hibernate-sessionfactory.html

Rahul Wagh
  • 380
  • 3
  • 15
3

Ya, Its very simple to understand that sessionFactory follow singleton design pattern. So you can create only one object in hole application. SessionFactory is also thread safe so only one thread can execute at a time its code. The instance of sessionFactory is heavyweighted because it contains connection, hibernate configuration, mapping files, location path so if you create number of instance of sessionFactory then your code becomes very heavy. Due to that reason we are using only one sessionFactory instance for one application. To improve performance of our application we are using only one instance of sessionFactory in one application.