gpt4 book ai didi

java - Spring Integration : javax. persistence.TransactionRequiredException:没有事务正在进行

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:41 26 4
gpt4 key购买 nike

我想在 Spring Integration 中实现以下目标:

  1. 在输入 channel 中收到消息
  2. 这会通过一个 Transformer 将其保存在数据库中,从而为其提供一个 ID
  3. 它会继续在某些外部系统中进行进一步处理。

我的变压器看起来像这样:

@PersistenceContext
private EntityManager em;

@Transactional
public ServiceResponse persistBatchOperation(List<OperationDTO> operations) {

for (OperationDTO op : operations) {
em.persist(op);
}
em.flush();

...其余省略

em.flush() 抛出此异常:

    org.springframework.integration.transformer.MessageTransformationException: org.springframework.integration.MessageHandlingException: javax.persistence.TransactionRequiredException: no transaction is in progress

我的集成上下文包含以下元素:

<tx:annotation-driven />

<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

AspectJ 部分:

<tx:advice id="txGatewayJPA" transaction-manager="jpaTransactionManager">
<tx:attributes>
<tx:method name="*HLR*" />
</tx:attributes>
</tx:advice>

<aop:pointcut id="allServices"
expression="execution(* com.mypackage.IntegrationService.*(..))" />
<aop:advisor advice-ref="txGatewayJPA" pointcut-ref="allServices" />
</aop:config>

Hibernate 和数据库配置:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="hiperPU" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>

<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="POSTGRESQL" />
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

<bean id="dataSource" class="***.ConfigRoutingDatasource">
<property name="configName" value="$hiperConfig{DATASOURCE_NAME_DATA}" />
<property name="defaultTargetDataSource">
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url"
value="jdbc:postgresql://XXX" />
<property name="username" value="XXX" />
<property name="password" value="XXX" />
<property name="maxActive" value="50" />
<property name="maxIdle" value="50" />
<property name="maxWait" value="600000" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="select version()" />
<property name="defaultTransactionIsolation"
value="#{ T(java.sql.Connection).TRANSACTION_READ_COMMITTED }" />
</bean>
</property>
</bean>

在我看来,我已经配置了使我的转换器方法具有事务性的所有内容。为什么不起作用?

最佳答案

尝试 em.merge(op);

添加@Transactional(readOnly = false)

并尝试使用

EntityManager em = emf.createEntityManager();

关于java - Spring Integration : javax. persistence.TransactionRequiredException:没有事务正在进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21904037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com