本文介绍了春季批,XA和本地交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在春季批处理中拥有一个JobRepository以使用本地事务,但是在分布式XA事务中执行特定的作业步骤?

It is possible to have a jobRepository in spring batch to use local transactions but execute particular job steps in distributed XA transaction?

对于XA,我使用Atomicos 3.8.0.步骤应该在处理后读取JMS消息并更新数据库.

For XA I use Atomicos 3.8.0. Step is supposed to read the JMS message and update the DB after processing.

弹簧配置的相关部分:

<job id="job" xmlns="http://www.springframework.org/schema/batch">
     <step id="inventorySync">
         <tasklet transaction-manager="xaTransactionManager">
            <chunk reader="jmsQueueReader"
                   processor="messageProcessor"
                   writer="dbWriter"
                   reader-transactional-queue="true"/>
         </tasklet>
     </step>
</job>

    <bean id="xaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"
          lazy-init="true" depends-on="inventoryDataSource">
        <constructor-arg name="transactionManager" ref="userTransactionManager"/>
        <constructor-arg name="userTransaction" ref="userTransaction"/>
        <property name="allowCustomIsolationLevels" value="true"/>
    </bean>

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    <property name="dataSource" ref="batchJobsDataSource"/>
    <property name="transactionManager" ref="transactionManager"/>
    <property name="databaseType" value="${batch.data.source.type}"/>
</bean>

    <jdbc:embedded-database id="batchJobsDataSource" type="HSQL"/>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="batchJobsDataSource"/>
    </bean>

推荐答案

在成功在单元测试中使用Bitronix和在生产中使用WebSphere JTA之前,我已经在所有这三种资源中使用了XA事务.如果在其余步骤中有单独的批处理数据库事务管理器,则在以下任何一个位置(批处理数据库或步骤)发生故障的情况下,都有可能使步骤和批处理数据库不同步" ).

I have used an XA transaction across all 3 of those resources before successfully using Bitronix in unit testing and WebSphere JTA in production. If you have a separate transaction manager for the Batch Database from the rest of your step, you risk the step and batch database being 'out-of-sync' in the event of a failure in either one of the places (batch database or step).

例如,您可能在步骤(JMS和DB)上成功提交,然后在批处理数据库上失败.当您重新开始工作时,它会认为某个特定步骤是不成功的,而您的基础执行却失败了.在同一个事务管理器中覆盖所有3种资源将防止这种情况.

for example, you may have a successful commit on your step (JMS and DB) and then failure on your batch Database. when you restart your job it will think that a certain step was unsuccessful whilst your underlying execution was. covering all 3 of the resources in the same transaction manager will prevent this.

这篇关于春季批,XA和本地交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:22