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!