Hibernate Search 的 Initial MassIndexer 不适用于 Wepshphere 8.5,但在 TOMCAT 上运行良好

Hibernate Search's Initial MassIndexer not working with Wepshphere 8.5, but works fine on TOMCAT

我们正在尝试使用休眠搜索创建初始弹性搜索索引。

以下是我们系统上的配置和我们面临的异常。

首先配置: hibernate.cfg.xml: jdbc/dataSource

    <property name="dialect">com.csc.pt.hiber.MSSQLDialect2012</property>
    <!-- Session/Transaction -->

    <property name="hibernate.current_session_context_class">thread</property>
    <property name="transaction.jta.platform" >org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform</property>
    <property name="transaction.manager_lookup_class">org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</property>

    <!-- Use Scrollable Result Set -->
    <property name="hibernate.jdbc.use_scrollable_resultset">true</property>
    <!--  more verbose logs -->
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.format_sql">false</property>
    <property name="hibernate.use_sql_comments">false</property>
    <property name="hibernate.generate_statistics">false</property>
    <property name="hibernate.query.startup_check">false</property>

    <!-- <property name="hibernate.connection.autocommit">true</property>  -->
    <property name="connection.isolation">1</property>

    <!-- performance factors -->
    <property name="hibernate.jdbc.batch_size">50</property>
    <property name="hibernate.default_batch_fetch_size">8</property>
    <!-- FSIT#179159 Resolution#65256 - Start -->
    <property name="hibernate.order_inserts">true</property>
    <!-- FSIT#179159 Resolution#65256 - End -->

    <!--  second level cache -->
    <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
    <property name="hibernate.cache.use_minimal_puts">true</property>
    <property name="hibernate.cache.use_query_cache">false</property>
    <property name="hibernate.cache.use_second_level_cache">false</property>

<property name="hibernate.search.default.indexmanager">elasticsearch</property>
        <property name="hibernate.search.default.elasticsearch.host">http://ip:9400</property>
         <property name="hibernate.search.default.elasticsearch.index_schema_management_strategy">drop-and-create</property>
         <property name="hibernate.search.default.elasticsearch.required_index_status">yellow</property>
         <property name="hibernate.search.default.indexwriter.infostream">true</property>

我们使用的 Hibernate 和 Hibernate Search 版本:

class 的代码,用于创建初始索引:

@SuppressWarnings("static-access")
    public static String createElasticSearchIndex(HibernateSessionFactory hsf) {
        Session session = hsf.current().getSession("");
        FullTextSession fullTextSession = Search.getFullTextSession(session.getSession());
        try {
            fullTextSession.createIndexer().startAndWait();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

现在我们面临的异常。

[10/14/18 15:55:56:029 EDT] 00000393 SystemOut     O [org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer](ERROR): HSEARCH000065: Error while rolling back transaction after null
java.lang.UnsupportedOperationException
    at org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter.rollback(WebSphereExtendedJtaPlatform.java:116)
    at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.rollbackTransaction(IdentifierConsumerDocumentProducer.java:201)
    at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.loadList(IdentifierConsumerDocumentProducer.java:181)
    at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.loadAllFromQueue(IdentifierConsumerDocumentProducer.java:140)
    at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.run(IdentifierConsumerDocumentProducer.java:120)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522)
    at java.util.concurrent.FutureTask.run(FutureTask.java:277)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.lang.Thread.run(Thread.java:785)
[10/14/18 15:55:56:029 EDT] 00000393 SystemOut     O [org.hibernate.search.exception.impl.LogErrorHandler](ERROR): HSEARCH000058: HSEARCH000212: An exception occurred while the MassIndexer was transforming identifiers to Lucene Documents
java.lang.UnsupportedOperationException
    at org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter.begin(WebSphereExtendedJtaPlatform.java:91)
    at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.beginTransaction(IdentifierConsumerDocumentProducer.java:191)
    at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.loadList(IdentifierConsumerDocumentProducer.java:164)
    at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.loadAllFromQueue(IdentifierConsumerDocumentProducer.java:140)
    at org.hibernate.search.batchindexing.impl.IdentifierConsumerDocumentProducer.run(IdentifierConsumerDocumentProducer.java:120)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522)
    at java.util.concurrent.FutureTask.run(FutureTask.java:277)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.lang.Thread.run(Thread.java:785)
[10/14/18 15:55:56:057 EDT] 00000380 SystemOut     O [org.hibernate.search.exception.impl.LogErrorHandler](ERROR):
 HSEARCH000058: HSEARCH000211: An exception occurred while the MassIndexer was fetching the primary identifiers list
org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [java:comp/websphere/ExtendedJTATransaction]
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:100)
    at org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter$TransactionAdapter.<init>(WebSphereExtendedJtaPlatform.java:138)
    at org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter$TransactionAdapter.<init>(WebSphereExtendedJtaPlatform.java:134)
    at org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter.getTransaction(WebSphereExtendedJtaPlatform.java:106)
    at org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter.getStatus(WebSphereExtendedJtaPlatform.java:101)
    at org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform.getCurrentStatus(AbstractJtaPlatform.java:136)
    at org.hibernate.internal.StatelessSessionImpl.flushBeforeTransactionCompletion(StatelessSessionImpl.java:659)
    at org.hibernate.internal.StatelessSessionImpl.beforeTransactionCompletion(StatelessSessionImpl.java:641)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access0(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
    at org.hibernate.search.batchindexing.impl.IdentifierProducer.inTransactionWrapper(IdentifierProducer.java:121)
    at org.hibernate.search.batchindexing.impl.IdentifierProducer.run(IdentifierProducer.java:89)
    at org.hibernate.search.batchindexing.impl.OptionallyWrapInJTATransaction.runWithErrorHandler(OptionallyWrapInJTATransaction.java:70)
    at org.hibernate.search.batchindexing.impl.ErrorHandledRunnable.run(ErrorHandledRunnable.java:33)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522)
    at java.util.concurrent.FutureTask.run(FutureTask.java:277)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.lang.Thread.run(Thread.java:785)
Caused by: javax.naming.ConfigurationException: A JNDI operation on a "java:" name cannot be completed because the server runtime is not able to associate the operation's thread with any J2EE application component.  This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request.  Make sure that a J2EE application does not execute JNDI operations on "java:" names within static code blocks or in threads created by that J2EE application.  Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on "java:" names. [Root exception is javax.naming.NameNotFoundException: Name comp/websphere not found in context "java:".]
    at com.ibm.ws.naming.java.javaURLContextImpl.throwExceptionIfDefaultJavaNS(javaURLContextImpl.java:522)
    at com.ibm.ws.naming.java.javaURLContextImpl.throwConfigurationExceptionWithDefaultJavaNS(javaURLContextImpl.java:552)
    at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:481)
    at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485)
    at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:396)
    at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:157)
    at javax.naming.InitialContext.lookup(InitialContext.java:432)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:97)
    ... 21 more
Caused by: javax.naming.NameNotFoundException: Name comp/websphere not found in context "java:".
    at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1970)
    at com.ibm.ws.naming.ipbase.NameSpace.retrieveBinding(NameSpace.java:1377)
    at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1220)
    at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1142)
    at com.ibm.ws.naming.urlbase.UrlContextImpl.lookupExt(UrlContextImpl.java:1436)
    at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:477)

Please let me know, if you require any more information.

经过长时间的分析,努力,终于实现了JPA(RESOURCE_LOCAL)。试图从我当前的 Hibernate Session 中获取实体管理器并开始分析和搜索以找到执行此操作的正确方法,并找到了它:)。

通过 SO 的 thread 上的 post 获得了我一直在寻找的完美答案。

这很有魅力。我只需要用前面提到的线程下的代码替换我的旧代码。

使用 MassIndexer 创建索引的旧代码:

 Session session = hibernateSessionFactory.current().getSession("");
 FullTextSession fullTextSession = Search.getFullTextSession(session);
 fullTextSession.createIndexer().startAndWait();

修改后的代码如下:

Session session = hibernateSessionFactory.current().getSession("");
 EntityManager em = session.getEntityManagerFactory().createEntityManager();
 FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
 fullTextEntityManager.createIndexer().startAndWait();

希望对您有所帮助!!