Getting SessionFactory/Session in WebApplication

I have code like below:

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.

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()

This is my HibernateUtil

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()//
	            // 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


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

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