Announcement Announcement Module
Collapse
No announcement yet.
Posted this in wrong forum last time accidently Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Posted this in wrong forum last time accidently

    Ok, this seems to be a re-occuring problem that no-one can answer. I have a swing app, that has a second thread to do long tedious retrievals so as not to tie up my gui.

    I have a facade class that has a getAllProducts() method. This method implements a cache layer that looks similiar to this:


    Code:
    public List<Product> getAllProducts&#40;&#41; &#123;
       if&#40;allProducts == null&#41; &#123;
            allProducts = new ArrayList<Product>&#40;&#41;;
            allProducts = dao.getAllProducts&#40;&#41;;
       &#125;
       return allProducts;
    &#125;
    This enables me to only retrieve the products one time. Now, within my spawned thread that does long updates, I need to access these products similiar to:

    Code:
    
                    for &#40;MarketingResponsibility resp &#58; facade
                            .getAllMarketingResponsibility&#40;&#41;&#41; &#123;
                        try &#123;
                            products = facade.getProducts&#40;resp&#41;;
                            reports = createReports&#40;products&#41;;
                            runExport&#40;fileLocation + resp.getMktgRespDesc&#40;&#41;&#41;;
                        &#125; catch &#40;NoDataFoundException ndfe&#41; &#123;/**/
                        &#125;
    
                    &#125;
    This is causing a lazilyinitialization error, and I don't undestand why because the session never gets closed.

    Code:
        private void runExport&#40;String location&#41; throws NoDataFoundException&#123;
            application.startSession&#40;&#41;;
    // this is the call that spawns the new thread
            processManager.runExport&#40;false,location&#41;;
    // this does some GUI stuff while the export is running
            monitorProgress&#40;&#41;;
            facade.deleteAllPriceImpactData&#40;&#41;;
            facade.deleteAllPricesAndCostData&#40;&#41;;
            application.endSession&#40;&#41;;
        &#125;
    
    
    
        public int startSession&#40;&#41; throws TransactionException &#123;
            try &#123;
                log.warn&#40;"getting session"&#41;;
                HibernateSession.currentSession&#40;appContext&#41;;
                return 1;
            &#125; catch &#40;HibernateException he&#41; &#123;
                log.error&#40;"cant get session"&#41;;
                throw new TransactionException&#40;&#41;;
            &#125;
        &#125;
    
        public void endSession&#40;&#41; throws TransactionException &#123;
            try &#123;
                log.warn&#40;"ending session"&#41;;
                HibernateSession.closeSession&#40;&#41;;
            &#125; catch &#40;HibernateException he&#41; &#123;
                log.error&#40;"cant close session"&#41;;
                throw new TransactionException&#40;&#41;;
            &#125;
        &#125;



    here is how i am handling my sessions



    Code:
    public class Application &#123;
    public int startSession&#40;&#41; throws TransactionException &#123;
          try &#123;
                log.warn&#40;"getting session"&#41;;
                HibernateSession.currentSession&#40;appContext&#41;;
                return 1;
            &#125; catch &#40;HibernateException he&#41; &#123;
                log.error&#40;"cant get session"&#41;;
                throw new TransactionException&#40;&#41;;
            &#125;
        &#125;
    
        /**
         *
         * @return boolean
         */
        public boolean isSessionOpen&#40;&#41; &#123;
            return HibernateSession.isSessionOpen&#40;&#41;;
        &#125;
    
        /**
         *
         * @throws TransactionException
         */
        public void endSession&#40;&#41; throws TransactionException &#123;
            try &#123;
                log.warn&#40;"ending session"&#41;;
                HibernateSession.closeSession&#40;&#41;;
            &#125; catch &#40;HibernateException he&#41; &#123;
                log.error&#40;"cant close session"&#41;;
                throw new TransactionException&#40;&#41;;
            &#125;
        &#125;
    
    
    
    
    
    public class HibernateSession &#123;
    
        /**
         *
         */
        public static final ThreadLocal<Session> session = new ThreadLocal<Session>&#40;&#41;;
    
        private static SessionFactory sessionFactory;
    
        /**
         * Get the current Session or create a new sesion is none exists
         *
         * @param appContext
         * @return Session
         * @throws HibernateException
         */
        public static Session currentSession&#40;ApplicationContext appContext&#41;
                throws HibernateException &#123;
            Session s = session.get&#40;&#41;;
            if &#40;s == null&#41; &#123;
                sessionFactory = &#40;SessionFactory&#41; appContext
                        .getBean&#40;"sessionFactory"&#41;;
                s = sessionFactory.openSession&#40;&#41;;
                session.set&#40;s&#41;;
                if &#40;!TransactionSynchronizationManager.getResourceMap&#40;&#41;
                        .containsKey&#40;sessionFactory&#41;&#41; &#123;
                    TransactionSynchronizationManager.bindResource&#40;sessionFactory,
                            new SessionHolder&#40;s&#41;&#41;;
                &#125;
            &#125;
    
            return s;
        &#125;
    
        /**
         *
         * @return boolean
         */
        public static boolean isSessionOpen&#40;&#41; &#123;
            return &#40;session.get&#40;&#41; != null&#41;;
        &#125;
    
        /**
         * Close the current Session
         *
         * @throws HibernateException
         */
        public static void closeSession&#40;&#41; throws HibernateException &#123;
    
            Session s = session.get&#40;&#41;;
            session.set&#40;null&#41;;
            if &#40;s == null&#41;
                System.err.println&#40;"big problem"&#41;;
            if &#40;s != null&#41; &#123;
    
                s.flush&#40;&#41;;
    
                TransactionSynchronizationManager.unbindResource&#40;sessionFactory&#41;;
                SessionFactoryUtils.closeSessionIfNecessary&#40;s, sessionFactory&#41;;
            &#125;
    
        &#125;
    
    &#125;

    Any thoughts or suggestions would be a huge help

    -Nick
    http://www.nickchristy.blogspot.com

  • #2
    problem solved. The issue was not with spring, nor hibernate, i was using SwingWorker incorrectly by placing "executable" code within a constructor rather then within the construct() method, the session was not opening correctly because of this, which was causing the Lazy exception

    Comment

    Working...
    X