Announcement Announcement Module
Collapse
No announcement yet.
LazyInitializationException within transaction boundaries Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitializationException within transaction boundaries

    Hi,

    I've posted a couple of times about this problem, but still haven't resolved it, so I'll try and distill it down to it's simplest form.
    I'm using Spring 1.2.X and have a class MyService whose process() method is proxied by a TransactionProxyFactoryBean.

    Code:
    class MyService() {
    
      private void MyDao dao;
    
      public void process() {
        Customer customer = dao.getBestCustomer();
        customer.getOrders();
      }
    }
    When I call customer.getOrders() I get a LazyInitializationException indicating that there is no session available. How do I ensure that the Hibernate session is available to all code executed within process()?

    BTW, the DAO shown above implements HibernateDaoSupport, but is not proxied by a TransactionProxyFactoryBean.

    Many thanks in advance,
    DM

  • #2
    Posting a new thread each other day about the same subject isn't going to help you.

    The situation you draw here isn't anywhere near to the situation you have posted in the other threads.

    Comment


    • #3
      You can use a transaction manager:

      Code:
      class MyService() implements InitializingBean{
        private PlatformTransactionManager platformTransactionManager;
        private TransactionTemplate transactionTempate;
        private void MyDao dao;
      
        public void afterPropertiesSet() throws Exception {
          transactionTempate = new TransactionTemplate(platformTransactionManager);
        }
      
        /**
         * @param platformTransactionManager
         *            The platformTransactionManager to set.
         */
        public void setPlatformTransactionManager(PlatformTransactionManager platformTransactionManager) {
          this.platformTransactionManager = platformTransactionManager;
        }
      
        public void process() {
         transactionTempate.execute(new TransactionCallbackWithoutResult() {
      
              public void doInTransactionWithoutResult(TransactionStatus status) {
          Customer customer = dao.getBestCustomer();
          customer.getOrders();
      
      
            });
        }
      }

      Comment

      Working...
      X