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

时间:2014-02-20 09:50:30

标签: java spring hibernate jpa

我想在Spring Integration中实现以下功能:

  1. 在输入通道中收到消息
  2. 这是通过一个Transformer,它将它持久存储在数据库中,因此给它一个ID
  3. 继续进一步处理一些外部系统。
  4. 我的变压器看起来像这样:

    @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和DB配置:

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

    在我看来,我已经配置了所有会使我的变换器方法成为事务的东西。为什么不起作用?

1 个答案:

答案 0 :(得分:0)

尝试em.merge(op);

添加@Transactional(readOnly = false)

并尝试

EntityManager em = emf.createEntityManager();