Announcement Announcement Module
Collapse
No announcement yet.
Lazy Loading/@Transactional - Closing Session issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy Loading/@Transactional - Closing Session issue

    Hi all,
    I have a very simple setup:
    A UserManager that calls a UserDAO to get all the users from the USER table.
    A User has a collection of settings that has to be lazy loaded.

    But when I ask the Manager for all the user I got this error:
    Code:
    ERROR: org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role..., no session or session was closed
    I use annotations driven transaction management in my config:
    PHP Code:
    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <
    bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <
    property name="sessionFactory" ref="sessionFactory"/>
    </
    bean>
    <
    tx:annotation-driven/> 
    And my Manager and DAO are wired up like that:
    PHP Code:
    <bean id="userManager" class="com.test.service.impl.UserManager">
        <
    property name="userDAO" ref="userDao" />
    </
    bean>
     <
    bean id="userDao" class="com.test.dao.impl.UserDAO">
        <
    property name="sessionFactory" ref="sessionFactory" />
    </
    bean>
    <
    bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <
    property name="dataSource" ref="dataSource"/>
        <
    property name="configLocation" value="/WEB-INF/conf/hibernate.cfg.xml"/>
    </
    bean
    Here is part of my UserManager:
    PHP Code:
    @Transactional
    public class UserManager implements IUserManager {

        private 
    IUserDAO dao;
         
        @
    Required
        
    public void setUserDAO(IUserDAO dao)
        {
            
    this.dao dao;
        }
        

        public List<
    UsergetUsers() {
            return 
    dao.getUsers();
        }
    ... 
    And part of my DAO:
    PHP Code:
    public class UserDAO extends HibernateDaoSupport implements IUserDAO {

        @
    Override
        
    public List<UsergetUsers() 
        {
            return 
    getHibernateTemplate().find("from User");
        }
    ... 
    When I turn debugging on I can see the following operations:
    Code:
    DEBUG: org.springframework.orm.hibernate3.HibernateTemplate - Not closing pre-bound Hibernate Session after HibernateTemplate
    DEBUG: org.springframework.orm.hibernate3.HibernateTransactionManager - Initiating transaction commit
    DEBUG: org.springframework.orm.hibernate3.HibernateTransactionManager - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@6bc839a]
    DEBUG: org.springframework.orm.hibernate3.HibernateTransactionManager - Closing Hibernate Session [org.hibernate.impl.SessionImpl@6bc839a] after transaction
    DEBUG: org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session
    ERROR: org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.test.model.pojo.User.settings, no session or session was closed
    I am running out of ideas on how to get this work. Isn't the whole point of using transactions to keep the session alive?
    I think I am missing something here...

  • #2
    Your transaction is over so there is no need anymore for the session. The tx (and session) is closed AFTER the call to the getUsers() method. If you want to keep the session open you will need something like the OpenSessionInViewFilter.

    Comment


    • #3
      Yeah I have stumbled upon that OpenSessionInViewFilter thing but, I am using Flex for the front end application (using granite data services as the middleware).
      I wasn't sure if OpenSessionInViewFilter was usable in my case, but it actually appears to be...
      I'll give it a shot, thanks for the clarification

      Comment

      Working...
      X