-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Recursive One-to-Many Tree: LazyInitializationException
PostPosted: Wed Aug 08, 2007 3:02 pm 
Newbie

Joined: Thu Jun 21, 2007 2:10 pm
Posts: 4
Hibernate version: 2

I have a recursive one-to-many tree structure that needs to be walked from top-to-bottom. In walking the tree, I use the following code (simplified):

Inside service bean:
Code:
    public Document getOrgGroupDocument() {
        ...
        // rootOrgGroupList has 1 item in it
        List<OrgGroup> rootOrgGroupList = getRootOrgGroupList();
        addOrgGroupElements(rootOrgGroupList, doc);
        ...
    }

    private void addOrgGroupElements(List<OrgGroup> orgGroupList, Document doc)
    {
        for (int i = 0; i < orgGroupList.size(); i++) {
            OrgGroup orgGroup = orgGroupList.get(i);
            ...
            addOrgGroupElementByUser(orgGroup.getChildOrgGroups(), doc);
            ...
        }
    }


Mapping documents:

Code:
@Entity
@Table(name = "FL_ORG_GROUP")
public class OrgGroup
{
    ...
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_OID", referencedColumnName = "CHRONICLE_OID")
    public List<OrgGroup> getChildOrgGroups()
    {
        return childOrgGroup;
    }
    ...
}


Session management:

I'm using org.springframework.orm.hibernate3.support.OpenSessionInViewFilter and org.springframework.transaction.interceptor.TransactionProxyFactoryBean to proxy my service bean.

applicationContext.xml:
Code:
    <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/FrontlineDS"/>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="jndiDataSource"/>
        <property name="configLocation" value="hibernate.cfg.xml"/>
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="homeAmImpl" class="model.module.home.HomeModuleImpl">
        <property name="orgGroupMgr" ref="orgGroupManager"/>
    </bean>
    <bean id="homeAm"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="target" ref="homeAmImpl"/>
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <bean id="orgGroupManager"
        class="com.frontlinemci.model.entity.orggroup.OrgGroupManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


Problem:

Stepping through with the debugger (and as you can see in the log output below), getRootOrgGroupList() fires off "FROM OrgGroup WHERE parentOid IS NULL", then the recursive method gets called for the first time. orgGroup.getChildOrgGroups() is lazy loaded (annotation shown above) and on the 2nd recursive call orgGroupList gets loaded with "load one-to-many model.entity.orggroup.OrgGroup.childOrgGroup". Finally, on the 3rd recursive call, when orgGroupList.size() gets called, Hibernate throws a LazyInitializationException.

It seems very strange to me that this exceptions occurs the 2nd time the lazy collection is used, NOT the first time. I'm afraid this may indicate there is something fundamentally wrong with my session handling, but I've been over the documentation again and again. Any help or suggestions is greatly appreciated.

Code:
Hibernate: /* FROM OrgGroup WHERE parentOid IS NULL */ select orggroup0_.ROW_OID as ROW1_47_, orggroup0_.CHRONICLE_OID as CHRONICLE2_47_, orggroup0_.CREATED_BY as CREATED3_47_, orggroup0_.MODIFIED_BY as MODIFIED4_47_, orggroup0_.VALID_BEGIN_DT as VALID5_47_, orggroup0_.VALID_END_DT as VALID6_47_, orggroup0_.DESC_TX as DESC7_47_, orggroup0_.ORG_GROUP_ID as ORG8_47_, orggroup0_.PARENT_OID as PARENT9_47_, orggroup0_.PRIMARY_CONTACT_OID as PRIMARY10_47_, orggroup0_.UOS_TOTAL_FL as UOS11_47_ from FL_ORG_GROUP orggroup0_ where (orggroup0_.PARENT_OID is null)
Hibernate: /* load one-to-many com.frontlinemci.model.entity.orggroup.OrgGroup.childOrgGroup */ select childorggr0_.PARENT_OID as PARENT9_1_, childorggr0_.ROW_OID as ROW1_1_, childorggr0_.ROW_OID as ROW1_47_0_, childorggr0_.CHRONICLE_OID as CHRONICLE2_47_0_, childorggr0_.CREATED_BY as CREATED3_47_0_, childorggr0_.MODIFIED_BY as MODIFIED4_47_0_, childorggr0_.VALID_BEGIN_DT as VALID5_47_0_, childorggr0_.VALID_END_DT as VALID6_47_0_, childorggr0_.DESC_TX as DESC7_47_0_, childorggr0_.ORG_GROUP_ID as ORG8_47_0_, childorggr0_.PARENT_OID as PARENT9_47_0_, childorggr0_.PRIMARY_CONTACT_OID as PRIMARY10_47_0_, childorggr0_.UOS_TOTAL_FL as UOS11_47_0_ from FL_ORG_GROUP childorggr0_ where childorggr0_.PARENT_OID=?
Aug 8, 2007 10:38:33 AM org.hibernate.LazyInitializationException <init>
SEVERE: failed to lazily initialize a collection of role: model.entity.orggroup.OrgGroup.childOrgGroup, no session or session was closed

_________________
http://www.mikwat.com


Top
 Profile  
 
 Post subject: Complete Stack Trace
PostPosted: Wed Aug 08, 2007 3:19 pm 
Newbie

Joined: Thu Jun 21, 2007 2:10 pm
Posts: 4
Just wanted to add the complete stack trace:

Code:
SEVERE: failed to lazily initialize a collection of role: model.entity.orggroup.OrgGroup.childOrgGroup, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.entity.orggroup.OrgGroup.childOrgGroup, no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
   at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
   at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
   at model.module.home.HomeModuleImpl.addOrgGroupElements(HomeModuleImpl.java:369)
   at model.module.home.HomeModuleImpl.addOrgGroupElements(HomeModuleImpl.java:393)
   at model.module.home.HomeModuleImpl.addOrgGroupElements(HomeModuleImpl.java:393)
   at model.module.home.HomeModuleImpl.getOrgGroupDocument(HomeModuleImpl.java:343)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:296)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:177)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy28.getOrgGroupDocument(Unknown Source)
   at action.home.MyHomeAction.prepareMyDeptPortlet(MyHomeAction.java:166)
   at action.home.MyHomeAction.preparePage(MyHomeAction.java:145)
   at action.FrontlineBaseAction.execute(FrontlineBaseAction.java:58)
   at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
   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:269)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at servlet.FrontlineSessionFilter.doFilter(FrontlineSessionFilter.java:75)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
   at java.lang.Thread.run(Thread.java:595)

_________________
http://www.mikwat.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 9:18 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I've just built a skeleton app that mirrors your situation: tomcat, osiv, spring tx, hibernate v3.2.5ga, recursive tree traversal. I'm not getting any problems. You obviously have a session at the start and then either it disappears or gets closed. Suggest you put a breakpoint inside hibernate's SessionImpl.close() to see what's doing the closing. In my case osiv closes the session as expected when jsp finished rendering.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 11:54 am 
Newbie

Joined: Thu Jun 21, 2007 2:10 pm
Posts: 4
Thanks for your help! It's reassuring to know that I haven't stumbled across a limitation of Hibernate.

My current work-around is to fire off a
Code:
getHibernateTemplate().find("FROM OrgGroup WHERE parentOid=?", chronicleOid);
call instead of using the one-to-many association.

I will try a few more tests and post the results.

_________________
http://www.mikwat.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 12:03 pm 
Newbie

Joined: Thu Jun 21, 2007 2:10 pm
Posts: 4
Hey thatmikewilliams, would you mind posting the source code of your skeleton app? I'm still struggling with this one.

I've put a breakpoint in SessionImpl.close() and it's never reached, but in AbstractPersistentCollection.readSize() the session variable is null.

Thanks!

_________________
http://www.mikwat.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 12:58 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
email or message me and I'll post you a zip


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.