-->
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.  [ 5 posts ] 
Author Message
 Post subject: Search throwsLazy loading exception on save in scheduled job
PostPosted: Thu Aug 11, 2011 2:15 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi,

I replaced Hibernate Search 3.3.0 with 3.4.0 in order to get rid of some lazy loading exceptions I got on collections. But now I'm seeing a new one.
This occurs on a Quartz scheduled job. As part of the job an object/entity is saved that is @Indexed. When it does that I get this exception

Code:
org.springframework.orm.hibernate3.HibernateSystemException: Error while indexing in Hibernate Search (before transaction completion); nested exception is org.hibernate.HibernateException: Error while indexing in Hibernate Search (before transaction completion)
   at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:273)
   at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
   at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
   at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: org.springframework.orm.hibernate3.HibernateSystemException: Error while indexing in Hibernate Search (before transaction completion); nested exception is org.hibernate.HibernateException: Error while indexing in Hibernate Search (before transaction completion)
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
   at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
   at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy121.aggregateStatistics(Unknown Source)
   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:597)
   at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
   at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)
   ... 3 more
Caused by: org.hibernate.HibernateException: Error while indexing in Hibernate Search (before transaction completion)
   at org.hibernate.search.backend.impl.EventSourceTransactionContext$DelegateToSynchronizationOnBeforeTx.doBeforeTransactionCompletion(EventSourceTransactionContext.java:175)
   at org.hibernate.engine.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:543)
   at org.hibernate.engine.ActionQueue.beforeTransactionCompletion(ActionQueue.java:216)
   at org.hibernate.impl.SessionImpl.beforeTransactionCompletion(SessionImpl.java:571)
   at org.hibernate.jdbc.JDBCContext.beforeTransactionCompletion(JDBCContext.java:250)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:138)
   at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
   ... 18 more
Caused by: org.hibernate.search.bridge.BridgeException: Exception while calling bridge#set
   class: nl.project.model.JobOffer
   field bridge:
   at org.hibernate.search.bridge.util.ContextualExceptionBridge.buildBridgeException(ContextualExceptionBridge.java:78)
   at org.hibernate.search.bridge.util.ContextualExceptionBridge.set(ContextualExceptionBridge.java:86)
   at org.hibernate.search.engine.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:465)
   at org.hibernate.search.engine.DocumentBuilderIndexedEntity.getDocument(DocumentBuilderIndexedEntity.java:443)
   at org.hibernate.search.engine.DocumentBuilderIndexedEntity.createAddWork(DocumentBuilderIndexedEntity.java:380)
   at org.hibernate.search.engine.DocumentBuilderIndexedEntity.addWorkToQueue(DocumentBuilderIndexedEntity.java:358)
   at org.hibernate.search.engine.WorkPlan$PerEntityWork.enqueueLuceneWork(WorkPlan.java:449)
   at org.hibernate.search.engine.WorkPlan$PerClassWork.enqueueLuceneWork(WorkPlan.java:250)
   at org.hibernate.search.engine.WorkPlan.getPlannedLuceneWork(WorkPlan.java:150)
   at org.hibernate.search.backend.WorkQueue.prepareWorkPlan(WorkQueue.java:134)
   at org.hibernate.search.backend.impl.BatchedQueueingProcessor.prepareWorks(BatchedQueueingProcessor.java:124)
   at org.hibernate.search.backend.impl.PostTransactionWorkQueueSynchronization.beforeCompletion(PostTransactionWorkQueueSynchronization.java:89)
   at org.hibernate.search.backend.impl.EventSourceTransactionContext$DelegateToSynchronizationOnBeforeTx.doBeforeTransactionCompletion(EventSourceTransactionContext.java:172)
   ... 24 more
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: nl.project.model.JobOffer.educationTypes, no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
   at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
   at nl.project.dao.hibernate.bridge.JobOfferClassBridge.set(JobOfferClassBridge.java:30)
   at org.hibernate.search.bridge.util.ContextualExceptionBridge.set(ContextualExceptionBridge.java:83)
   ... 35 more


Since the other code in the scheduled job interacts normally with the session, I'm assuming this is directly related to Search.
I call the method through the service layer, so there is a transaction present

The dao code is this
Code:
String sql = "Select s.offerId,count(s.id) as counter,max(s.dte) etc [...] ";

Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
ScrollableResults result = session.createSQLQuery(sql)
   .addScalar("maxDte",StandardBasicTypes.TIMESTAMP)
   .scroll(ScrollMode.FORWARD_ONLY);

int count = 0;
Date dte = null;
while(result.next()){
   Offer off = offerDao.get((Long) result.get(0));
   off.setViews(off.getViews() + (Integer) result.get(1));
   
   if(++count %20 == 0){
      session.flush();
      session.clear();
   }
}


Any ideas on this one?

Kind regards,
Marc


Top
 Profile  
 
 Post subject: Re: Search throwsLazy loading exception on save in scheduled job
PostPosted: Thu Aug 11, 2011 7:45 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi Marc,
Let's check the obvious first: the Hibernate Session is closed before the transaction is committed?
sorry if it's silly, but it seems to be a common issue with Spring users.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Search throwsLazy loading exception on save in scheduled job
PostPosted: Thu Aug 11, 2011 7:48 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
sorry, answering at 1am so I didn't see the DAO code.

when flushing, and before clear(), make sure to use

Code:
org.hibernate.search.FullTextSession.flushToIndexes()


otherwise when the transaction commits you'll still have pending changes in the transaction context, but the related entities are detached from the Session by the clear operation.
So make sure to flush the index queue too before clearing the session.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Search throwsLazy loading exception on save in scheduled job
PostPosted: Mon Aug 15, 2011 2:49 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
thanks!


Top
 Profile  
 
 Post subject: Re: Search throwsLazy loading exception on save in scheduled job
PostPosted: Sun Sep 25, 2011 10:36 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi Sanne,

I still seem to have this issue:
Code:
ScrollableResults result = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql)
   .addScalar("offerId",StandardBasicTypes.LONG)
   .addScalar("counter",StandardBasicTypes.INTEGER)
   .addScalar("maxDte",StandardBasicTypes.TIMESTAMP)
   .scroll(ScrollMode.FORWARD_ONLY);

int count = 0;
Date dte = null;
while(result.next()){
   Offer off = offerDao.get((Long) result.get(0));
   
   if(++count %20 == 0){
      Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
      Search.getFullTextSession(session).flushToIndexes();
      session.flush();
      session.clear();
   }
}

sql = "delete from mytble where dte <= :dte";
getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql)
.setDate("dte", dte)
.executeUpdate();



The error is throw in a classbridge that is trying to retrieve a lazy collection. The classbirdge is executed as a result of a quartz task

I'm getting

Code:
nested exception is org.hibernate.search.bridge.BridgeException: Exception while calling bridge#set
        class: nl.myproject.model.Offer
        field bridge:
        at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:273)
        at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: org.hibernate.search.bridge.BridgeException: Exception while calling bridge#set
        class: nl.myproject.model.Offer
        field bridge:
        at org.hibernate.search.bridge.util.ContextualExceptionBridge.buildBridgeException(ContextualExceptionBridge.java:78)
        at org.hibernate.search.bridge.util.ContextualExceptionBridge.set(ContextualExceptionBridge.java:86)
[...]
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: nl.myproject.User.roles, no session or session was closed
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)



Any idea how I might resolve this?

Kind regards,
Marc


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.