Announcement Announcement Module
Collapse
No announcement yet.
Lazy Load exception using OSIV Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy Load exception using OSIV

    Hello,
    I am using OSIVInterceptor and usually this works fine. However there are a couple of classes that intermittently throw hibernate lazy load exceptions even though they are wired to the OSIV. I also don't see anything that could be detaching my objects in the code. I don't see a log that the Hibernate session was closed before the error is thrown. Usually I see this logged when the OSIV closes the session.

    Any help would be greatly appreciated!
    web.xml
    Code:
        <filter>
            <filter-name>requestContextFilter</filter-name>
            <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>requestContextFilter</filter-name>
            <url-pattern>/dwr/*</url-pattern>
        </filter-mapping>
        
        <!--
            The 'contextConfigLocation' parameter tells Spring's ContextLoaderListener which Spring
            configuration files to load.
        -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/application-context.xml
            </param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
             
    <servlet>
      <servlet-name>lp</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    application-context.xml
    Code:
     	<bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
      		<property name="flushModeName">
      			<value>FLUSH_AUTO</value>
      		</property>
      		<property name="sessionFactory">
      			<ref bean="sessionFactory"/>
      		</property>
      		<property name="singleSession">
      		  		<value>true</value>
      		</property>
    
      	</bean>
    	 <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
        <!-- HIBERNATE STUFF -->
    	<!-- LocalSessionFactoryBean loads one or more HIbernate mapping XML files to produce a Hibernate Session Factory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource"/></property>
        <property name="mappingResources">
                <list>
               		...hbm.xmls mapped here
                </list>
        </property>
        <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                    <prop key="hibernate.max_fetch_depth">3</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
        </property>
        
        <property name="lobHandler"><ref bean="oracleLobHandler"/></property>
    	</bean>
    Offending code
    Code:
    public Lesson copyAndReturnNewLesson(Integer lessonIdToCopy, User user) throws Exception
    	{
    		Lesson newLesson = new Lesson();
    		Lesson lessonToCopy = mLessonDao.findById(lessonIdToCopy);
    		Integer arcId = lessonToCopy.getArc().getId();
    		BeanUtils.copyProperties(newLesson, lessonToCopy);
    		newLesson.setId(null);
    		newLesson.setName(lessonToCopy.getName() + " (Copy)");
    		newLesson.setCreatedBy(null);
    		newLesson.setCreatedDate(null);
    		LPUtilities.setAuditColumns(user.getUserID().toString(), newLesson);
    		newLesson.setAssessments(new ArrayList());
    		newLesson.setLessonTeks(new ArrayList());
    		newLesson.setRelatedLessonTeks(new ArrayList());
    		newLesson.setResources(new ArrayList());
    		newLesson.setStages(new ArrayList());
    		for(Resource res: lessonToCopy.getResources())
    		{
    			newLesson.getResources().add(res);
    		}
    		this.addArc(newLesson, arcId);
    		mLessonDao.save(newLesson);
    //**works if move here**//
    		mLessonDao.flushHibSession();
    		for(LessonTeks lt: lessonToCopy.getLessonTeks())
    		{
    			LessonTeks newLessonTeks = new LessonTeks();
    			BeanUtils.copyProperties(newLessonTeks, lt);
    			newLessonTeks.setId(null);
    			newLessonTeks.setLesson(newLesson);
    			mLessonTeksDao.save(newLessonTeks);
    			newLessonTeks.setStudentExpectations(new ArrayList());
    			mLessonDao.flushHibSession();
    			for(TeksStudentExpectation se: lt.getStudentExpectations())
    			{
    				newLessonTeks.getStudentExpectations().add(se);
    			}		
    			mLessonTeksDao.save(newLessonTeks);
    		}
    		for(RelatedLessonTeks rlt: lessonToCopy.getRelatedLessonTeks())
    		{
    			RelatedLessonTeks newRelatedLessonTeks = new RelatedLessonTeks();
    			BeanUtils.copyProperties(newRelatedLessonTeks, rlt);
    			newRelatedLessonTeks.setId(null);
    			newRelatedLessonTeks.setLesson(newLesson);
    			newRelatedLessonTeks.setStudentExpectations(new ArrayList());
    			mRelatedLessonTeksDao.save(newRelatedLessonTeks);
    		}
    //BOMBS on next line with lazy load exception
    //The really STRANGE part is if I move the following block up to //**works if move here**//, I do not receive the exception
    		for(Stage stg: lessonToCopy.getStages())
    		{
    			Stage newStage = new Stage();
    			BeanUtils.copyProperties(newStage, stg);
    			newStage.setId(null);
    			newStage.setCreatedBy(null);
    			newStage.setCreatedDate(null);
    			LPUtilities.setAuditColumns(user.getUserID().toString(), newStage);
    			newStage.setLesson(newLesson);
    			mStageDao.save(newStage);
    		}
    		return newLesson;		
    	}
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.austinisd.lp.db.model.Lesson.stages, no session or session was closed
    org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationException(AbstractPersi stentCollection.java:358)
    org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationExceptionIfNotConnected (AbstractPersistentCollection.java:350)
    org.hibernate.collection.AbstractPersistentCollect ion.initialize(AbstractPersistentCollection.java:3 43)
    org.hibernate.collection.AbstractPersistentCollect ion.read(AbstractPersistentCollection.java:86)
    org.hibernate.collection.PersistentBag.iterator(Pe rsistentBag.java:246)
    org.austinisd.lp.service.managers.LessonManager.co pyAndReturnNewLesson(LessonManager.java:154)
    org.austinisd.lp.web.controllers.CopyLessonControl ler.handle(CopyLessonController.java:41)
    org.springframework.web.servlet.mvc.AbstractComman dController.handleRequestInternal(AbstractCommandC ontroller.java:84)
    org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:153)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:48)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:874)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:808)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:476)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:431)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
    org.austinisd.lp.framework.LoginCheckFilter.doFilt er(LoginCheckFilter.java:142)
    Last edited by litmusunit; Jul 16th, 2008, 03:53 PM. Reason: inclusion of error Message

  • #2
    More information for LazyInit issue

    Any help would be greatly appreciated. I've been pouring over the forums and documentation for a couple of days now to no avail.

    I wanted to include a copy of my error log as well. The code is executed in a service layer class, LessonManager. I am wondering if only my DAO's and view layer do not have transactional support from OSIV because of some misconfiguration on my part.
    BTW using Spring 2.5 and Hibernate 3.0

    Comment


    • #3
      Originally posted by litmusunit View Post
      Any help would be greatly appreciated. I've been pouring over the forums and documentation for a couple of days now to no avail.

      I wanted to include a copy of my error log as well. The code is executed in a service layer class, LessonManager. I am wondering if only my DAO's and view layer do not have transactional support from OSIV because of some misconfiguration on my part.
      BTW using Spring 2.5 and Hibernate 3.0
      Have you solved the problem?

      Comment


      • #4
        Here is an explanation for the behaviour you see.

        Here is an explanation for the behaviour you see.

        I had the same problem and it took me a while to figure out what was going on.

        Comment


        • #5
          I am getting the same exception. How did you resolve it? Any help is highly appreciated.

          Thanks,
          Gupta

          Comment


          • #6
            Well it depends on what your code is trying to accomplish, but I guess you have a few options:
            • don't call clear()
            • don't try to lazily initialise any collection on the object after calling clear()
            • either re-attach or re-load the object from hibernate, and then lazily initialise the collection

            Comment


            • #7
              How do I lazily initialize the collection?

              Thanks,
              Gupta

              Comment


              • #8
                Follow up

                I never solved this problem but by moving the placement of my code as detailed in my code snippet, I was able to get it to work "well enough for now". I don't think danielalexiuc's explanation solves my problem as I'm never clearing the session, only flushing it. I'd still love to know what the issue is as this has come back to bite me more than once in this method.

                pnvsgupta, did you ever solve this?
                Thanks,
                Kathy

                Comment

                Working...
                X