facebook

[Closed] Cannot get session from HibernateSessionFacotry…

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #263914 Reply

    Hi folks,

    I’m trying to track down a problem that I’m having on one of my websites using Hibernate. Every once in a while (4 times in the last 24) my Hibernate based web application stops working. I’ll ask the hosting provider what is going on and to check the logs for me (they don’t allow access to the Tomcat logs) and they’ll report seeing the following error:

    
    
    
    14:54:37,948 ERROR [action]:253 - Servlet.service() for servlet action threw exception
    
    javax.servlet.jsp.JspException: ServletException in '/content-pages/public/classes/schedule-overview.jsp': Got a null back from the HibernateSessionFactory
    
            at org.apache.struts.taglib.tiles.InsertTag$InsertHandler.doEndTag(InsertTag.java:923)
    
            at org.apache.struts.taglib.tiles.InsertTag.doEndTag(InsertTag.java:462)
    
            at org.apache.jsp.layouts.public_jsp._jspx_meth_tiles_insert_2(org.apache.jsp.layouts.public_jsp:465)
    
            at org.apache.jsp.layouts.public_jsp._jspService(org.apache.jsp.layouts.public_jsp:241)
    
            at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
    
            at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
    
            at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    
            at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
    
            at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
    
            at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
    
            at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
    
            at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
    
            at org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
    
            at org.apache.struts.tiles.TilesRequestProcessor.processTilesDefinition(TilesRequestProcessor.java:239)
    
            at org.apache.struts.tiles.TilesRequestProcessor.internalModuleRelativeForward(TilesRequestProcessor.java:341)
    
            at org.apache.struts.action.RequestProcessor.processForward(RequestProcessor.java:560)
    
            at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:209)
    
            at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    
            at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    
            at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:307)
    
            at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:385)
    
            at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:748)
    
            at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:678)
    
            at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:871)
    
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    
            at java.lang.Thread.run(Thread.java:595)
    
    

    The actual error is being emitted by my own code when it encounters a condition where the session it gets back from the HibernateSessionFactory is of a null value. The HibernateSessionFactory is the stock code that MyEclipse generates when adding Hibernate capabilites to a project.

    What would cause the session factory to return a null value and is there a way for me to prevent that very bad behavior from happening in some code that I write?

    #263915 Reply

    eh.. excuse the fat fingers on the title.. 🙁

    #263922 Reply

    Haris Peco
    Member

    Peter,

    There is an issue with thread handling in current MyEclipse’s session factory.We have fixed it and it will be in next release.
    You can change HibernateSessionFactory with next :

    
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    
    /**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html }.
     */
    public class HibernateSessionFactory {
    
        /** 
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;
    
        static {
            try {
                configuration.configure(configFile);
                sessionFactory = configuration.buildSessionFactory();
            } catch (Exception e) {
                System.err
                        .println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
        private HibernateSessionFactory() {
        }
        
        /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
    
            if (session == null || !session.isOpen()) {
                if (sessionFactory == null) {
                    rebuildSessionFactory();
                }
                session = (sessionFactory != null) ? sessionFactory.openSession()
                        : null;
                threadLocal.set(session);
            }
    
            return session;
        }
    
        /**
         *  Rebuild hibernate session factory
         *
         */
        public static void rebuildSessionFactory() {
            try {
                configuration.configure(configFile);
                sessionFactory = configuration.buildSessionFactory();
            } catch (Exception e) {
                System.err
                        .println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
    
        /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);
    
            if (session != null) {
                session.close();
            }
        }
    
        /**
         *  return session factory
         *
         */
        public static org.hibernate.SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        /**
         *  return session factory
         *
         *    session factory will be rebuilded in the next call
         */
        public static void setConfigFile(String configFile) {
            HibernateSessionFactory.configFile = configFile;
            sessionFactory = null;
        }
    
        /**
         *  return hibernate configuration
         *
         */
        public static Configuration getConfiguration() {
            return configuration;
        }
    
    

    or for jdk 1.4 :

    
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    
    /**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html }.
     */
    public class HibernateSessionFactory {
    
        /** 
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        private static final ThreadLocal threadLocal = new ThreadLocal();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;
    
        static {
            try {
                configuration.configure(configFile);
                sessionFactory = configuration.buildSessionFactory();
            } catch (Exception e) {
                System.err
                        .println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
        private HibernateSessionFactory() {
        }
        
        /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
    
            if (session == null || !session.isOpen()) {
                if (sessionFactory == null) {
                    rebuildSessionFactory();
                }
                session = (sessionFactory != null) ? sessionFactory.openSession()
                        : null;
                threadLocal.set(session);
            }
    
            return session;
        }
    
        /**
         *  Rebuild hibernate session factory
         *
         */
        public static void rebuildSessionFactory() {
            try {
                configuration.configure(configFile);
                sessionFactory = configuration.buildSessionFactory();
            } catch (Exception e) {
                System.err
                        .println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
    
        /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);
    
            if (session != null) {
                session.close();
            }
        }
    
        /**
         *  return session factory
         *
         */
        public static org.hibernate.SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        /**
         *  return session factory
         *
         *    session factory will be rebuilded in the next call
         */
        public static void setConfigFile(String configFile) {
            HibernateSessionFactory.configFile = configFile;
            sessionFactory = null;
        }
    
        /**
         *  return hibernate configuration
         *
         */
        public static Configuration getConfiguration() {
            return configuration;
        }
    
    }

    Hope it helps and sorry for the inconvenience caused.

    Regards,

    #263923 Reply

    Thanks for the update folks. I’ve installed the new HibernateSessionFactory. Where does the template file for this live so I can update the template so I don’t run into this again…

    #263924 Reply

    Haris Peco
    Member

    Peter,

    Attached is sessionfactory2.vm.zip.You can unpack this file and svae them in MYECLIPSE_HOME/eclipse/plugins/com.genuitec.eclipse.hibernate_5.1.0/templates directory

    Regards,

    Attachments:
    You must be logged in to view attached files.
    #263929 Reply

    thank you very much Peco!

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: [Closed] Cannot get session from HibernateSessionFacotry…

You must be logged in to post in the forum log in