-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hibernate Search Index Not created for Spring JTA
PostPosted: Tue Sep 22, 2009 2:51 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

Sorry to be back again..but I've hit a problem. This may be something for Spring or it may be something for Hibernate so I'm not sure. It seems as though indexing does not occur when using Spring managed JTA. Using Hibernate transaction manager provided by Spring works fine. When using JTA and asking hibernate to participate in the JTA ,the result is that the work to be processed to create the index does not work on a commit.

I realise this has been mentioned before and sorry for bringing this up. I am trying to find a way to resolve this and have looked at numerous configurations but no luck. My manager has come up with an interceptor that checks for a commit and the calls the FullTextSessionImpl and invoke flushToIndexes(). I'm not too keen on this and just wondering if anyone may have got this working? I tried adding a jta transaction manager to the sessionfactory but i get some side effects (session closed lazyinitialisation exception).

I would be grateful for any input (even if I'm being dumb!).

Cheers


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Wed Sep 23, 2009 6:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

no dumb question at all. People keep getting caught in this. I thing this article - http://blog.xebia.com/2008/07/18/config ... g-for-jta/ - explains it quite well. The problem is that Hibernate needs to be able to detect the running transaction.

Depending on your configuration and version of spring the solution might look different. How does your Spring config look like (no guarantee that I can help though ;-))

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Wed Sep 23, 2009 3:23 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi Hardy

Thanks for getting back to me. I tried the thing that the blogger mentioned but there was some unwanted side effects for example we have mapping definitions which as lazy="true" and attaching a jta manager to the session factory causes lazy initialisation exceptions. At home with the demo project i get an out of memory error when i attach a jta manager.

The only way we managed to get it working was by implementing an EmptyInterceptor and use Spring's TransactionSynchronizationAdapter to register tx and then do a flush to index. Here is the same code:

Code:
public class SearchTransactionInterceptor extends EmptyInterceptor implements BeanFactoryAware, InitializingBean {

   private BeanFactory beanFactory;
   private static final Logger log = Logger.getLogger(SearchTransactionInterceptor.class);
   
   private final TransactionSynchronizationAdapter transactionSynchronizationAdapter;
   
   public SearchTransactionInterceptor() {
      transactionSynchronizationAdapter = new TransactionSynchronizationAdapter() {
            @Override
            public void beforeCommit(boolean readonly) {
               flushToIndex();
            }

            @Override
            public void afterCompletion(int status) {
                if (status == STATUS_ROLLED_BACK)
                    log.error("Transaction rolled back after email messages were queued. EXPECT LOST EMAILS");
            }

        };
   }
   
    private void registerTransactionSynchronisation() {
        // TransactionSynchronization is per thread so no need for synchronised block here
        // otherwise would need to synchronise this method
        List<?> synchronizations = TransactionSynchronizationManager.getSynchronizations();
        if (!synchronizations.contains(transactionSynchronizationAdapter)) {
            TransactionSynchronizationManager.registerSynchronization(transactionSynchronizationAdapter);
        }
   }
   
   public void flushToIndex() {
      registerTransactionSynchronisation();
      log.debug("Flushing lucene work to indexes.");
      SessionFactory sessionFactory = (SessionFactory) beanFactory.getBean("sessionFactory");
      Session session = sessionFactory.getCurrentSession();
      FullTextSession fullTextSession = Search.getFullTextSession(session);
      fullTextSession.flushToIndexes();
   }

   @Override
   public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
      this.beanFactory = beanFactory;
   }

   @Override
   public void afterPropertiesSet() throws Exception {
      
   }
   
   @Override
   public boolean onLoad(Object entity, Serializable id, Object[] state,
         String[] propertyNames, Type[] types) {
      if (TransactionSynchronizationManager.isSynchronizationActive()) {
         registerTransactionSynchronisation();
      }
      return super.onLoad(entity, id, state, propertyNames, types);
   }
}



This seems to work. To be honest I'm not really happy with the above solution.

I have a demo project which doesn't use the above but shows the problem that I am experiencing and I would be happy to send it to you or someone who may be able to help.

On a side note just wanted to say that we did show and tell of what we've done so far with Hibernate Search and our product and all the members were extremely pleased. So thanks guys!


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Thu Sep 24, 2009 5:28 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

my first question is - do you really need a JTA transaction manager? Do you really have to synchronize across multiple resources. If not, it might be easier to just switch the transaction manager.

Regarding the interceptor. You are inversing the logic of Hibernate Search a little. Per default, the indexing is done in afterCompletion (see also PostTransactionWorkQueueSynchronization in the HSearch codebase). The idea is that no index changes occur if a transaction rollback occurs. Only after a successful commit changes get applied to the index. The worst case scenario is then that an exception occurs during indexing and you end up with a index which is not up to date anymore. Otherwise the interceptor idea should work.

Personally, I would further investigate how to make Hibernate fully aware of the JTA transaction using the properties hibernate.transaction.manager_lookup_class and hibernate.transaction.factory_class. The values to use depend on the container you are running in. If you read for example this blog - http://ourcraft.wordpress.com/tag/jta/ - especially the blog entry "Blowing up in test mode".

Hope this helps.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Thu Sep 24, 2009 6:10 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

We do need JTA because we have more than one datasource and jms that we work with. I will look into that. Just to complicate things more we have legacy code that creates entries in the db directly. As a result I am creating another action that manually indexes our object. For example:

Code:
          Long id  = getIdFromLegacy(<param>);
      
       FullTextSession fullTextSession = Search.getFullTextSession(session);
       Entity entity  = entityService.findById(id  );
       fullTextSession.index(entity  );
       fullTextSession.flushToIndexes();
       LOGGER.debug("issued reindex of entity  : " + id);


Think this project is getting complicated by the minute. If I try teh above it will still work with JMS configuration I presume.


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Thu Sep 24, 2009 6:23 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
If I try teh above it will still work with JMS configuration I presume.


Not sure what you mean with "the above". If you mean using hibernate.transaction.manager_lookup_class and hibernate.transaction.factory_class - I guess so, but I have no idea about your use case and what your JMS queue is actually for, etc
There is a lot of context you have to take into consideration for such a complicated setup.

Regarding the manual indexing. That should be fine.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Thu Sep 24, 2009 6:55 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
hardy.ferentschik wrote:
Quote:
If I try teh above it will still work with JMS configuration I presume.


Not sure what you mean with "the above". If you mean using hibernate.transaction.manager_lookup_class and hibernate.transaction.factory_class - I guess so, but I have no idea about your use case and what your JMS queue is actually for, etc
There is a lot of context you have to take into consideration for such a complicated setup.

Regarding the manual indexing. That should be fine.

--Hardy


Sorry what i meant to say was if i have a jms configuration (master slave) and if i do a manual index in the code will this create a lucenework and place on the queue for processing by the master?

Sorry for being in 2 places today.

Cheers


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Thu Sep 24, 2009 8:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Provided you manual configuration code uses the configuration for a JMS slave it is ok.


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Thu Sep 24, 2009 9:02 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Thanks!


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Fri Sep 25, 2009 3:53 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Adding this link as cross reference - http://forum.springsource.org/showthread.php?t=78204
See if anything comes from the springforum.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Thu Oct 08, 2009 6:47 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

I think we have fixed the problem. We've done the following:

Code:
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JOTMTransactionManagerLookup</prop>   
               
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>


This seems to cause the ebvent listeners to register the tx. However we have noticed a side effect of adding this change with teh following exception:
Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.xyz.domain.Company.users, no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
   at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
   at org.hibernate.search.engine.DocumentBuilderContainedEntity.processContainedIn(DocumentBuilderContainedEntity.java:585)
   at org.hibernate.search.engine.DocumentBuilderContainedEntity.addWorkToQueue(DocumentBuilderContainedEntity.java:564)
   at org.hibernate.search.backend.impl.BatchedQueueingProcessor.addWorkToBuilderQueue(BatchedQueueingProcessor.java:162)
   at org.hibernate.search.backend.impl.BatchedQueueingProcessor.processWorkByLayer(BatchedQueueingProcessor.java:140)
   at org.hibernate.search.backend.impl.BatchedQueueingProcessor.prepareWorks(BatchedQueueingProcessor.java:129)
   at org.hibernate.search.backend.impl.PostTransactionWorkQueueSynchronization.beforeCompletion(PostTransactionWorkQueueSynchronization.java:47)
   at org.objectweb.jotm.SubCoordinator.doBeforeCompletion(SubCoordinator.java:1487)
   at org.objectweb.jotm.SubCoordinator.commit_one_phase(SubCoordinator.java:416)
   at org.objectweb.jotm.TransactionImpl.commit(TransactionImpl.java:239)
   at org.objectweb.jotm.Current.commit(Current.java:488)
   at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1028)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:709)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:678)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)


I could be wrong but i would['ve thought that the session would be closed becuase we're doing a afterCommit indexing. Also the entire tx is rolled back due to the lucne document creation. I was hoping to avoid this as we don't want our users work to be rolled back because we couldn't index the data.

The main thing I would like to resolve is the lazyinitialisation exception. I'm hoping im nearly there, i would like to avoid any custom hacks to get this to work...any help as usual would be appreciated.


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Thu Oct 08, 2009 11:09 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Apologies for coming back again, but I've been banging my head all day and just wondering if anyone might be able to shed some light on the problem.

Cheers


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Fri Oct 09, 2009 6:48 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

Unfortunately I am still stuck on this same problem. I can't see how this would work because the transaction is committed and the session is closed so if the object is proxied then there is no way to load it. Is there something going on under the hood for example is HSearch creating a new session? How does indexing work if you are using proxied objects?

It's been over 2 days now...any help would be appreciated.

Thanks


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Fri Oct 09, 2009 9:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

the session should only be closed after the transaction commit. Where and how do you open/close the session?
Can you turn on debug trace to see where and when session/transaction begin/end? Unfortunately, I am not a JTA expert, especially not in connection with Spring.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search Index Not created for Spring JTA
PostPosted: Fri Oct 09, 2009 10:23 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

We've found what the problem is. And it was related to your tx set up and not using the standard OpenViewSessionFilter. It's been resolved. I will post something on my blog to show what the problem was and how we fixed it. Sorry guys for hounding you. I haven't slept in 2 days so it's been painful. Spring has been very disappointing in helping with this.


Thanks again


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.