我有一个 Spring 配置的 Web 应用程序,它通过以下侦听器接收 JMS 消息:
public class EntityPersister implements MessageListener {
@Resource
private EntityManager entityManager;
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
Object entity = createEntity(textMessage);
entityManager.persist(entity);
entityManager.flush(); //for debugging only
}
}
}
当我在我的应用程序中执行此侦听器时,我NoTransactionException
从行中得到一个entityManager.flush()
.
我需要配置什么才能让实体管理器参与到已经存在的 JTA 事务中?
我已经尝试@Transactional
过上述实现,但没有成功。
ActiveMQ 用作 JMS 提供程序。弹簧配置为:
<bean id="jmsConnectionFactory" class="com.atomikos.jms.AtomikosConnectionFactoryBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="atomikos-activemq" />
<property name="xaConnectionFactory">
<!-- ActiveMQ wird als JMS Provider genutzt -->
<bean id="activeMQXAConnectionFactory"
class="org.apache.activemq.spring.ActiveMQXAConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
</property>
<property name="maxPoolSize" value="2" />
<property name="localTransactionMode" value="false" />
</bean>
<bean id="entityPersister" class="EntityPersister" />
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destinationName" ref="entityDestinationName" />
<property name="messageListener" ref="entityPersister" />
<property name="sessionTransacted" value="true" />
<property name="transactionManager" ref="txManager" />
</bean>
OpenJPA 用作 JPA 提供者。持久性单位是:
<persistence-unit name="somePU" transaction-type="JTA">
<jta-data-source>managedDataSource</jta-data-source>
<non-jta-data-source>nonManagedDataSource</non-jta-data-source>
<!-- some entity class listed here -->
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
弹簧配置为:
<!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<!-- when close is called, should we force transactions to terminate or
not? -->
<property name="forceShutdown" value="true" />
</bean>
<!-- Also use Atomikos UserTransactionImp, needed to configure Spring -->
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
</bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="somePU" />
<property name="jpaPropertyMap">
<map>
<entry key="openjpa.ManagedRuntime" value="jndi" />
</map>
</property>
</bean>
<bean id="entityManager" factory-bean="entityManagerFactory"
factory-method="createEntityManager" />
OpenJPA 通过 JNDI 从持久性单元中查找 XA 和非 XA 数据源。