在ItemWriter中的异常之后没有发生Spring Batch和rollback

时间:2016-05-26 21:28:18

标签: java spring spring-batch batch-processing

我有chunk的Spring批处理作业并设置commit-interval =" 3"并声明了可跳过的异常。 我想在DB中保存3项: ITEM_1 ITEM_2 ITEM_3

说,我想将这些项目保存在数据库中,但第二项无效,并发生异常。在这种情况下应该回滚并且Spring Batch应该单独重新运行有问题块的每个项目,每个项目一次提交/事务。所以在数据库中应该保存item_1和item_3。

但在我保存的数据库中保存: ITEM_1 ITEM_1 ITEM_3 因此item_1是重复的。 看起来 item_1 在发生异常之前提交,或者在发生异常后没有回滚。

我整天都在这个案子上,但仍然不知道出了什么问题。

下面,Spring批处理作业配置。 你知道什么是错的吗?

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/batch
        http://www.springframework.org/schema/batch/spring-batch.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="marketData" class="net.skwaro.iftpp.model.MarketData" scope="prototype" />
    <bean id="itemProcessor" class="net.skwaro.iftpp.job.custom.CustomItemProcessor" scope="prototype" />
    <bean id="customStepListener" class="net.skwaro.iftpp.job.listeners.CustomStepListener"  scope="prototype" />
    <bean id="customProcessListener"  class="net.skwaro.iftpp.job.listeners.CustomProcessListener"  scope="prototype" />
    <bean id="customWriterListener" class="net.skwaro.iftpp.job.listeners.CustomWriterListener"  scope="prototype" />

    <batch:job id="fetchingMarketDataJob" >
        <batch:step id="step1" >
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="outputFileItemReader" writer="mysqlItemWriter" processor="itemProcessor" 
                    commit-interval="3" skip-limit="3" >
                    <batch:listeners>
                        <batch:listener ref="customStepListener" />
                        <batch:listener ref="customProcessListener" /> 
                        <batch:listener ref="customWriterListener" />
                    </batch:listeners>
                    <batch:skippable-exception-classes>
                        <batch:include class="java.lang.Exception" />
                        <batch:include class="java.sql.SQLException" />
                        <batch:include class="org.springframework.jdbc.UncategorizedSQLException" />
                    </batch:skippable-exception-classes> 

                </batch:chunk>


            </batch:tasklet>
        </batch:step>
    </batch:job>


    <bean id="outputFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" value="file:D:\WLS12c\output.txt" />
        <property name="lineMapper">
            <bean class="net.skwaro.iftpp.job.custom.CustomLineMapper">
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value="|"/>
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean class="net.skwaro.iftpp.job.mappers.MarketDataFieldSetMapper" />
                </property>
            </bean>
        </property>
    </bean>




    <bean id="mysqlItemWriter"
    class="org.springframework.batch.item.database.JdbcBatchItemWriter">
    <property name="dataSource" ref="dataSource" />
    <property name="sql">
      <value>
            <![CDATA[        
                insert into TEST_TABLE(NAME,SOURCE,MONEY_VALUE) 
            values (:name, :source, :moneyValue)
            ]]>
      </value>
    </property>
    <property name="itemSqlParameterSourceProvider">
        <bean
        class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
    </property>
  </bean>

</beans>

的applicationContext.xml

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/batch
        http://www.springframework.org/schema/batch/spring-batch.xsd">

    <task:annotation-driven />
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="net.skwaro.iftpp"></context:component-scan>

    <import resource="../WEB-INF/fetch-job.xml" />
    <context:property-placeholder location="classpath:configuration.properties"
        ignore-unresolvable="true" />

        <util:properties id="database" location="classpath:database.properties"/>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="#{database['jdbc.url']}" />
        <property name="username" value="#{database['jdbc.username']}" />
        <property name="password" value="#{database['jdbc.password']}" />
    </bean>

    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
    </bean>


    <!-- stored job-meta in database -->
    <!-- <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> <property name="transactionManager" 
        ref="transactionManager" /> <property name="databaseType" value="mysql" /> 
        </bean> -->

    <bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>

</beans>

0 个答案:

没有答案