Reputation: 5325
Why use only one SessionFactory Object per application? What are the advantages of using single session factory object per application?
Upvotes: 25
Views: 52571
Reputation: 31
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.
Upvotes: 3
Reputation: 494
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
Upvotes: 3
Reputation: 2770
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.
Upvotes: 42
Reputation: 90447
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.
Upvotes: 16