# Getting SessionFactory/Session in WebApplication

**URL:** <https://discourse.hibernate.org/t/getting-sessionfactory-session-in-webapplication/5069>\
**Category:** Hibernate ORM\
**Created:** [February 16, 2021, 11:18am UTC](https://discourse.hibernate.org/t/getting-sessionfactory-session-in-webapplication/5069 "2021-02-16T11:18:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Souvik\_Dasgupta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/souvik_dasgupta/32/1393_2.png) [@Souvik\_Dasgupta](https://discourse.hibernate.org/u/Souvik_Dasgupta)\
**Post date:** [February 16, 2021, 11:18am UTC](https://discourse.hibernate.org/t/getting-sessionfactory-session-in-webapplication/5069/1 "2021-02-16T11:18:14Z")

</div>

I have code like below:

```auto
public class HibernateListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        HibernateUtil.getSessionFactory(); // Just call the static initializer of that class    
    }

    public void contextDestroyed(ServletContextEvent event) {
        HibernateUtil.getSessionFactory().close(); // Free all resources
    }
}

```

This is configured in web.xml and loaded via my initialServlet. Problem is how do I get sessions in my DAO classes?  
Let me know if more info is required.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [February 17, 2021, 5:36pm UTC](https://discourse.hibernate.org/t/getting-sessionfactory-session-in-webapplication/5069/2 "2021-02-17T17:36:50Z")

</div>

Not sure what this `HibernateUtil` is doing exactly, but usually you create a `Session` from a `SessionFactory` by calling `openSession` on the `SessionFactory`, so you would do `SessionFactory#openSession()`

---

<div class="post-metadata">

**Author:** ![Souvik\_Dasgupta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/souvik_dasgupta/32/1393_2.png) [@Souvik\_Dasgupta](https://discourse.hibernate.org/u/Souvik_Dasgupta)\
**Post date:** [February 19, 2021, 6:42am UTC](https://discourse.hibernate.org/t/getting-sessionfactory-session-in-webapplication/5069/3 "2021-02-19T06:42:24Z")

</div>

This is my HibernateUtil

```auto
public class HibernateUtil {
	
	private HibernateUtil() {
		
	}
	
	 private static final SessionFactory sessionFactory;
	 
	    // Hibernate 5:
	    static {
	        try {
	            // Create the ServiceRegistry from hibernate.cfg.xml
	            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()//
	                    .configure("hibernate.cfg.xml").build();
	 
	            // Create a metadata sources using the specified service registry.
	            Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
	 
	            sessionFactory = metadata.getSessionFactoryBuilder().build();
	        } catch (Throwable ex) {
	         
	            System.err.println("Initial SessionFactory creation failed." + ex);
	            throw new ExceptionInInitializerError(ex);
	        }
	    }
	 
	    public static SessionFactory getSessionFactory() {
	        return sessionFactory;
	    }
	 
	    public static void shutdown() {
	        // Close caches and connection pools
	        getSessionFactory().close();
	    }

}

```

The problem is how do I get hold of the session from my app? It has been created while initializing Tomcat

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [February 19, 2021, 9:30am UTC](https://discourse.hibernate.org/t/getting-sessionfactory-session-in-webapplication/5069/4 "2021-02-19T09:30:39Z")

</div>

Like I wrote, call `HibernateUtil.getSessionFactory().getCurrentSession()`.
