Results 1 to 3 of 3
  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default How to access SessionFactory instance stored in SerlvetContextListener class

    I want my hibernate SessionFactory instance to be loaded when my application is deployed so I created a class named HibernateContext.

    Java Code:
    package org.LMS.Model;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    
    public class HibernateContext implements ServletContextListener{
    
    	SessionFactory sessionFactory;
    	@Override
    	public void contextDestroyed(ServletContextEvent arg0) 
    	{
    		sessionFactory = new Configuration ().configure ().buildSessionFactory ();
    	}
    
    	@Override
    	public void contextInitialized(ServletContextEvent arg0) 
    	{
    		sessionFactory.close();		
    	}
    
    }//end HibernateContext class
    I configured my web.xml file to reflect this change.
    Java Code:
     <!-- HibernateContext Listener Mapping -->
      <listener>
      	<listener-class>org.LMS.Model.HibernateContext</listener-class>
      </listener>
    My problem is that how can is access the SessionFactory throughout my application from the HibernateContext class.Please tell me if there
    is a better way of accomplishing this.

  2. #2
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: How to access SessionFactory instance stored in SerlvetContextListener class

    Ok I found a solution
    Java Code:
    package org.LMS.Model;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    
    public class HibernateContext implements ServletContextListener{
    
    	private static SessionFactory sessionFactory;
    	@Override
    	public void contextDestroyed(ServletContextEvent arg0) 
    	{
    		sessionFactory = new Configuration ().configure ().buildSessionFactory ();
    	}
    
    	@Override
    	public void contextInitialized(ServletContextEvent arg0) 
    	{
    		sessionFactory.close();		
    	}
    
    	public static SessionFactory getInstance () 
    	{
    		return sessionFactory;
    	}
    }//end HibernateContext class
    But now when I run the application i'm getting a 404 for all the jsp & servlets. How do I fix this

  3. #3
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: How to access SessionFactory instance stored in SerlvetContextListener class

    Just saw the problem. didn't even realize that I made such a silly mistake I had this:
    Java Code:
      public void contextDestroyed(ServletContextEvent arg0) 
        {
            sessionFactory = new Configuration ().configure ().buildSessionFactory ();
        }
     
        @Override
        public void contextInitialized(ServletContextEvent arg0) 
        {
            sessionFactory.close();     
        }
    So I was making a SessionFactory instance in my ContextDestroyed method and closing the resources in the contextInitalized method. What I should have done was:
    Java Code:
    	@Override
    	public void contextDestroyed(ServletContextEvent arg0) 
    	{
    		sessionFactory.close();
    	}
    
    	@Override
    	public void contextInitialized(ServletContextEvent arg0) 
    	{
    		sessionFactory = new Configuration ().configure ().buildSessionFactory ();
    	}

Similar Threads

  1. Replies: 4
    Last Post: 08-26-2012, 02:09 AM
  2. Passing instance of derived class as base class
    By goatjugsoup in forum New To Java
    Replies: 7
    Last Post: 08-01-2012, 06:15 AM
  3. Replies: 1
    Last Post: 04-21-2012, 07:03 PM
  4. instance access
    By sujaiyer85 in forum Advanced Java
    Replies: 2
    Last Post: 04-23-2008, 08:37 AM
  5. How to access array stored in database through JSTL
    By saurabh.joshi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-19-2007, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •