简体   繁体   中英

Spring Integration: javax.persistence.TransactionRequiredException: no transaction is in progress

I would like to achieve the following in Spring Integration:

  1. A message is received in an input channel
  2. This goes through a Transformer which persists it in the DB and thus gives it an ID
  3. it goes on to further processing into some external systems.

My transformer looks like this:

@PersistenceContext
private EntityManager em;

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

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

...rest omitted

This exception is being thrown on em.flush():

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

My integration context contains these elements:

<tx:annotation-driven />

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

The AspectJ part:

<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 and DB config:

<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>

It seems to me that I have configured everything what would make my transformer method transactional. Why isn't it working?

try em.merge(op);

add @Transactional(readOnly = false)

and try with

EntityManager em = emf.createEntityManager();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM