-->
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: Indexing database with JTA
PostPosted: Thu Sep 17, 2009 5:54 pm 
Newbie

Joined: Thu Sep 17, 2009 5:43 pm
Posts: 7
I'm in one of thus suboptimal setups where we're locally deploying in Tomcat, but production is in WebSphere. We're using the EntityManager instead of Hibernate Session.

I have code that does a great job of indexing my database when persistence.xml is using resource_local, but can't work with JTA. Specifically, the example code in Hibernate Search in Action, we're told to use FullTextEntityManager.getTransaction(), which will throw and IllegalStateException when a JTA EntityManager is in use.

Now I'm stuck, since I can't easily test locally and be sure if it'll work in JTA.

We're going to try to configure Tomcat to use JTA, but in the short term, can anyone explain how to implement the "index database" code for JTA EntityManager. Here's what I had, with the errant getTransaction() calls:

Code:
public class SearchService  {
   @SuppressWarnings("unchecked")
   public void indexDatabase() {

      int index = 0;

      FullTextEntityManager fullTextEntityManager = getFullTextEntityManager();
      EntityTransaction tx = fullTextEntityManager.getTransaction(); // bomb
      tx.begin();
      fullTextEntityManager.purgeAll(PersistentSession.class);
      fullTextEntityManager.flushToIndexes();
      fullTextEntityManager.getSearchFactory().optimize(
            PersistentSession.class);

      List<PersistentSession> sessions = fullTextEntityManager.createQuery(
            "select session from PersistentSession as session")
            .getResultList();
      for (PersistentSession session : sessions) {
         fullTextEntityManager.index(session);
         index++;
         if ((index % batchSize == 0)) {
            fullTextEntityManager.flush();
         }
      }
      fullTextEntityManager.getTransaction().commit(); // bomb
      fullTextEntityManager.close();
   }
}


Any help is appreciated!


Top
 Profile  
 
 Post subject: Re: Indexing database with JTA
PostPosted: Fri Sep 18, 2009 8:09 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
FullTextEntityManager.getTransaction(), which will throw and IllegalStateException when a JTA EntityManager is in use

This is not true, I always use JTA and this instruction is fine.
Please post the stacktrace from "bomb" and check your configuration, as there is something wrong elsewhere.

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


Top
 Profile  
 
 Post subject: Re: Indexing database with JTA
PostPosted: Fri Sep 18, 2009 11:53 am 
Newbie

Joined: Thu Sep 17, 2009 5:43 pm
Posts: 7
Interesting. Because throwing an IllegalStateException is the described behavior in the javadoc for
javax.persistence.EntityManager#getTransaction
Quote:
Throws:
IllegalStateException - if invoked on a JTA EntityManager.


And if you look at org.hibernate.ejb.AbstractEntityManagerImpl, that's exactly what it does:

Code:
public EntityTransaction getTransaction() {
     if ( transactionType == PersistenceUnitTransactionType.JTA ) {
          throw new IllegalStateException( "A JTA EntityManager cannot use getTransaction()" );
     }
     return tx;
}


And here's the stacktrace I get:

Code:
Caused by: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
        at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:324)
        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.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)

        at $Proxy312.getTransaction(Unknown Source)
        at org.hibernate.search.jpa.impl.FullTextEntityManagerImpl.getTransaction(FullTextEntityManagerImpl.java:177)
        at com.SearchService.indexDatabase(SearchService.java:129)
        at com.SearchService.afterPropertiesSet(SearchService.java:230)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
        ... 71 more


Top
 Profile  
 
 Post subject: Re: Indexing database with JTA
PostPosted: Fri Sep 18, 2009 12:58 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I'm terribly sorry, even reading and writing "JTA" I was thinking to something else. Too many posts and windows open today.

Code:
int index = 0;
FullTextEntityManager fullTextEntityManager = ...//inject? get it somehow
fullTextEntityManager.purgeAll(PersistentSession.class);
fullTextEntityManager.flushToIndexes();
fullTextEntityManager.getSearchFactory().optimize(PersistentSession.class);

List<PersistentSession> sessions = fullTextEntityManager.createQuery(
      "select session from PersistentSession as session")
      .getResultList();
for (PersistentSession session : sessions) {
      fullTextEntityManager.index(session);
      index++;
      if ((index % batchSize == 0)) {
         fullTextEntityManager.flushToIndexes(); //NOT flush() !
      }
}


You don't need to begin the transaction, and don't commit it. Also use flushToIndexes and not flush as I corrected above.
If you would like to try it, in trunk there is a new MassIndexer to reindex everything faster.

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


Top
 Profile  
 
 Post subject: Re: Indexing database with JTA
PostPosted: Fri Sep 18, 2009 2:48 pm 
Newbie

Joined: Thu Sep 17, 2009 5:43 pm
Posts: 7
Many thanks, Sanne, that works for us in both local_resource and JTA!


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.