When we are using more than one database in our application than we use the HibernateUtil class which is implemented based on singleton design pattern which insures that one and only one sessionFactory object will be created for entire application.Session factory objects are to be implemented using the singleton design pattern.
Instances of SessionFactory are thread-safe and typically shared throughout an application. 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.
So if you create a SessionFactory for every request , it implies that you are not using database connection pool to serve your request. 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.
No comments:
Post a Comment