-->
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: Hibernate Search not updating index on insert
PostPosted: Wed Mar 23, 2011 10:26 pm 
Newbie

Joined: Wed Mar 23, 2011 9:20 pm
Posts: 2
I'm having trouble getting Hibernate Search to update indexes automatically using the FullTextIndexEventListener with Hibernate Annotations.

I'm using Spring 3.0 with BTM for JTA.

I've narrowed down the issue to the PostTransactionWorkQueueSynchronization class.

When a record is updated JDBCContext.connectionCleanedUp() is called which has the code:

Code:
afterTransactionCompletion( false, null )
// Note : success = false, because we don't know the outcome of the transaction


This calls EventSourceTransactionContext.DelegateToSynchronizationOnAfterTx.doAfterTransactionCompletion(boolean success, SessionImplementor sessionImplementor) which has the code:

Code:
synchronization.afterCompletion( success ? Status.STATUS_COMMITTED : Status.STATUS_ROLLEDBACK );


This calls PostTransactionWorkQueueSynchronization.afterCompletion(int i) which has the code:

Code:
if ( Status.STATUS_COMMITTED == i ) {
            if ( log.isTraceEnabled() ) {
               log.trace(
                     "Processing Transaction's afterCompletion() phase for {}. Performing work.", this.toString()
               );
            }
            queueingProcessor.performWorks( queue );
         }
         else {
            if ( log.isTraceEnabled() ) {
               log.trace(
                     "Processing Transaction's afterCompletion() phase for {}. Cancelling work due to transaction status {}",
                     this.toString(),
                     i
               );
            }
            queueingProcessor.cancelWorks( queue );
         }


I'm struggling to see how to get around this, it seems like JDBCContext will always say that the transaction was a failure and the index won't get updated.

Does anyone have the FullTextIndexEventListener actually working with transactions?


Top
 Profile  
 
 Post subject: Re: Hibernate Search not updating index on insert
PostPosted: Thu Mar 24, 2011 5:47 am 
Hibernate Team
Hibernate Team

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

which versions of Search and Hibernate Core are you using? Which transaction manager are you using and how is it configured. If you search this forum you fill find many posts about problems with the transaction manager in conjunction with Hibernate and Spring. Maybe you could post your Spring configuration.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search not updating index on insert
PostPosted: Thu Mar 24, 2011 5:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
FYI - viewtopic.php?f=9&t=998155


Top
 Profile  
 
 Post subject: Re: Hibernate Search not updating index on insert
PostPosted: Thu Mar 24, 2011 6:26 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Does anyone have the FullTextIndexEventListener actually working with transactions?

Of course, almost everyone :) It was designed for transaction, until where recently actually the out-of-transaction wasn't supported.

Quote:
afterTransactionCompletion( false, null )

So you're forcing the transaction to fail? In that case it won't index of course!

Generally speaking I saw many people "unit testing" JPA by doing some operations without ever committing, to use rollback as a cleanup phase of the tests; I don't think that's a good idea, and of course you won't be able to test any after-commit event like fulltext indexing.

Maybe have a look into the sourcecode, the hibernate-search-integrationtest module actually has some tests using Spring 3.0 with BTM - they where contributed as well, I don't have any experience with Spring myself so I assume these tests are fine.

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


Top
 Profile  
 
 Post subject: Re: Hibernate Search not updating index on insert
PostPosted: Thu Mar 24, 2011 11:04 am 
Newbie

Joined: Wed Mar 23, 2011 9:20 pm
Posts: 2
Wow, you guys are quick on the response, that is great. So I actually found the issue and have it working now. The code that calls afterTransactionCompletion( false, null ) is in org.hibernate.jdbc.JDBCContext which is getting called from a org.hibernate.transaction.JDBCTransaction

Seeing that our configuration was creating JDBCTransaction's got me thinking, this should be using JTATransaction instead. So I tracked it back to our Hibernate/Spring configuration.

In hibernate.properties we had:

Code:
hibernate.transaction.manager_lookup_class=org.springframework.orm.hibernate3.LocalTransactionManagerLookup
hibernate.transaction.factory_class=org.springframework.orm.hibernate3.SpringTransactionFactory


SpringTransactionFactory always uses JDBCTransaction instead of JTATransaction so I looked into replacing this. Looking through the forum I found a similar post that recommends the following configuration for Bitronix:

Code:
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.BTMTransactionManagerLookup
hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory


These didn't quite fit what we needed because we're not using JNDI and both of these depend on some form of JNDI configuration. So I extended both of these to remove the JNDI requirements.

NonJndiBtmTransactionManagerLookup

Code:
public class NonJndiBtmTransactionManagerLookup extends BTMTransactionManagerLookup {

   public String getUserTransactionName() {
      return null;
   }
   
}


NonJndiBtmJTATransactionFactory

Code:
public class NonJndiBtmJTATransactionFactory extends JTATransactionFactory {

   @SuppressWarnings("unchecked")
   protected UserTransaction getUserTransaction() {
      try {
         Class clazz = Class.forName("bitronix.tm.TransactionManagerServices");
         return (UserTransaction) clazz.getMethod("getTransactionManager", null).invoke(null, null);
      }
      catch (Exception e) {
         throw new HibernateException( "Could not obtain BTM transaction manager instance", e );
      }
   }
   
}


Now with these in place JTATransaction's are being created and the FullTextIndexEventListener is getting the proper Status and updating the index as expected. Hopefully this helps someone else out there. Thanks guys for your input!


Top
 Profile  
 
 Post subject: Re: Hibernate Search not updating index on insert
PostPosted: Thu Mar 24, 2011 12:04 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Great analysis. I am sure this will help some people. As said, JTA + Spring + Hibernate is a re-occurring question on this forum ;-)


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.