0

I am developing a JSF Web Application which is using Hibernate. I have created a singleton class for Session Factory, but when do i need to close the session factory object? I am currently closing each Session object at end of DAO method. What about session factory?

Shivani Bhansali
  • 109
  • 1
  • 12

1 Answers1

0

SessionFactory -> Open it once during the web application start up and close it on webapp stop. ServletContextListener could be one place. That should be good enough.

  • As per https://stackoverflow.com/questions/32775599/how-would-i-obtain-sessionfactory-from-the-servletcontext-in-the-dao/49270145#49270145 the following thread using Servlet Context is not a good approach. Can you please let me know if there is any other way? – Shivani Bhansali Mar 14 '18 at 05:15
  • Will session factory get closed automatically if server is shut down? – Shivani Bhansali Mar 14 '18 at 05:20
  • If server is shutdown, it won't call close method automatically and entire JVM process is killed and so no objects exist. Going thru link https://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/SessionFactory.html#close() will help why we should close sessionfactory. – Madhusudana Reddy Sunnapu Mar 14 '18 at 09:46