2

有一个运行 XA 数据源的 Websphere AS 8.5。还有一个 Spring 应用程序必须在此环境中工作并连接到数据源。应用程序上下文由 servlet 加载,应用程序用于RmiExporters提供对某些服务(在单独的线程中共享)的 RMI 访问,并且所有 DAO 的方法都用 标记@Transactional

如果我尝试从主线程向数据库写入一些东西(例如在任何类的初始化期间),它可以正常工作。但是,如果我尝试通过 RMI 调用编写(或使用 HQL SELECT-query 读取)某些内容,我会看到一个异常:

org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [java:comp/websphere/ExtendedJTATransaction]
at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:68)
at org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter$TransactionAdapter.<init>(WebSphereExtendedJtaPlatform.java:156)
at org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter$TransactionAdapter.<init>(WebSphereExtendedJtaPlatform.java:152)
at org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter.getTransaction(WebSphereExtendedJtaPlatform.java:124)
at org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform$TransactionManagerAdapter.getStatus(WebSphereExtendedJtaPlatform.java:119)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115)
at org.hibernate.service.jta.platform.internal.TransactionManagerBasedSynchronizationStrategy.canRegisterSynchronization(TransactionManagerBasedSynchronizationStrategy.java:56)
at org.hibernate.service.jta.platform.internal.AbstractJtaPlatform.canRegisterSynchronization(AbstractJtaPlatform.java:148)
at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.attemptToRegisterJtaSync(TransactionCoordinatorImpl.java:240)
at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.pulse(TransactionCoordinatorImpl.java:268)
at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1202)
at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:178)
at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89)
at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:179)
at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:174)
at com.ibm.ws.jpa.management.JPAEMFactory.createEntityManager(JPAEMFactory.java:297)
at org.springframework.orm.jpa.EntityManagerFactoryUtils.doGetTransactionalEntityManager(EntityManagerFactoryUtils.java:202)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:211)
at com.sun.proxy.$Proxy414.persist(Unknown Source)

实际配置如下:

persistence.xml 中的持久性单元:

<provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/oracledatasource</jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="connection.pool.size" value="5" />
        <property name="current_session_context_class" value="managed" />
        <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
        <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory" />
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
        <property name="hibernate.cache.use_second_level_cache" value="false" />
        <property name="hibernate.cache.use_query_cache" value="false" />
        <property name="hibernate.generate_statistics" value="true" />
        <property name="hibernate.enable_lazy_load_no_trans" value="true" />
    </properties>

Spring 配置如下所示:

<tx:jta-transaction-manager />
<tx:annotation-driven />

<bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    <property name="persistenceUnits">
        <map>
            <entry key="pu1" value="persistence/pu1" />
        </map>
    </property>
</bean>
<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/pu1"/>

我可以尝试什么来解决这个问题?

4

1 回答 1

1

实际的解决方案是使用带有持久性单元JPATransactionManager的 Spring 。LOCAL_RESOURCE

于 2013-07-18T17:12:03.667 回答