Results 1 to 2 of 2

Thread: Posted this in wrong forum last time accidently

  1. #1
    Join Date
    Feb 2005
    Location
    baltimore, md
    Posts
    9

    Default 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
    Thanks,

    Nick

  2. #2
    Join Date
    Feb 2005
    Location
    baltimore, md
    Posts
    9

    Default

    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
    Thanks,

    Nick

Similar Threads

  1. Cannot insert time into Oracle?
    By pikopepper in forum Data
    Replies: 3
    Last Post: Jun 14th, 2005, 12:36 AM
  2. Replies: 8
    Last Post: May 10th, 2005, 06:10 PM
  3. Great Forum --- Thank You!
    By spring04 in forum Architecture
    Replies: 2
    Last Post: Apr 8th, 2005, 02:48 AM
  4. Replies: 0
    Last Post: Nov 5th, 2004, 04:33 PM
  5. Replies: 2
    Last Post: Sep 23rd, 2004, 11:03 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
  •