Results 1 to 7 of 7

Thread: Rollback the Transaction

  1. #1

    Default Rollback the Transaction

    Hi All,

    We have the following scenario:

    We have a job with ItemReader, Processor and Writer.

    ItemReader - read the data from database.

    The ItemProcessor processes the records(record multiple tables). During this processing if any business exception is thrown, currently processed records need to rollback. After the rollback of the chunk the batch job should continue with the next item.

    I am using org.springframework.orm.hibernate3.HibernateTransa ctionManager,
    Spring batch 2.1
    <code>
    <job id="Load_ReturnedJob" restartable="true"
    xmlns="http://www.springframework.org/schema/batch">

    <step id="Load_ReturnedStep">
    <tasklet>
    <chunk reader="commonListItemReader"
    processor="StopOrderProcessor"
    writer="commonListWriter" commit-interval="1">
    </chunk>
    <listeners>
    <listener ref="loadReturnedListener" />
    </listeners>
    </tasklet>
    </step>

    </job>

    </code>

    How to rollback the transaction?

  2. #2

    Default Rollback the Transaction

    I tried to use Programmatic transaction management still the transaction is committed.
    <code>

    <job id="Load_ReturnedJob" restartable="true"
    xmlns="http://www.springframework.org/schema/batch">

    <step id="Load_ReturnedStep">
    <tasklet>
    <chunk reader="commonListItemReader"
    processor="StopOrderProcessor"
    writer="commonListWriter" commit-interval="1">
    </chunk>
    <listeners>
    <listener ref="loadReturnedListener" />
    </listeners>
    </tasklet>
    </step>

    </job>

    <bean id="StopOrderProcessor"
    class="batch.processreturneddds.processor.LoadRetu rnedDDsSosProcessor">
    <property name="transactionTemplate">
    <ref bean="sharedTransactionTemplate" />
    </property>
    </bean>

    <bean id="sharedTransactionTemplate"
    class="org.springframework.transaction.support.Tra nsactionTemplate">
    <property name="isolationLevelName" value="ISOLATION_READ_UNCOMMITTED" />
    <property name="transactionManager">
    <ref bean="transactionManager" />
    </property>
    </bean>



    <bean id="loadReturnedDDsSosListener"
    class="batch.processreturneddds.listener.LoadRetur nedDDsSosListener">
    </bean>


    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager"
    lazy-init="true">
    <property name="sessionFactory">
    <ref bean="batchSessionFactory" />
    </property>
    <property name="nestedTransactionAllowed" value="true">
    </property>

    </bean>

    </code>


    <code>

    public class LoadReturnedDDsSosProcessor implements ItemProcessor<RunTaskListItemDo, RunTaskListItemDo> {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = Logger.getLogger(LoadReturnedDDsSosProcessor.class );

    /** The transaction template. */
    private TransactionTemplate transactionTemplate;

    /**
    * @param transactionTemplate the transactionTemplate to set
    */
    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
    this.transactionTemplate = transactionTemplate;
    }


    /**
    * Process Returned Direct Debit and Stop orders.
    *
    * @param runTaskListItemDo
    * the run task list item do
    * @return ReturnedDirectDebitRequestDo, the ReturnedDirectDebitRequestDo.
    * @throws Exception
    * the Exception.
    */
    public RunTaskListItemDo process(final RunTaskListItemDo runTaskListItemDo) throws Exception {

    LOGGER.debug("Entered LoadReturnedDDsSosProcessor.");
    transactionTemplate.setPropagationBehavior(Transac tionDefinition.PROPAGATION_NESTED);

    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

    protected void doInTransactionWithoutResult(TransactionStatus status) {
    try {
    // call business functionality

    } catch (DirectDebitException directDebitException) {
    LOGGER.error(directDebitException.getExceptionNoti ficationList().getExceptionNotification().get(0)
    .getMessage());
    status.setRollbackOnly();


    }
    }
    });

    return runTaskListItemDo;
    }

    }
    </code>

    How to rollback the chunk?

    Thanks for ur reply in advance.
    Last edited by arun4; Feb 13th, 2011 at 11:24 PM.

  3. #3
    Join Date
    Dec 2010
    Posts
    27

    Default

    You simply need to define a transaction manager for your tasklet in the step.

    <tasklet transaction-manager="transactionManager">
    ...
    </tasklet>
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    <property name="dataSource" ref="..." />
    </bean>

    With the Spring transaction manager defined, execute() of the tasklet runs as a Spring transactional method. By default, an exception thrown in execute() will roll back all the SQL statements executed in the transactional execute().

  4. #4

    Default

    Thanks for your reply.

    We have the following scenario:

    We have a job with ItemReader, Processor and Writer.

    ItemReader - read the data from database.

    The ItemProcessor processes the records(record multiple tables). During this processing if any business exception is thrown, currently processed records need to rollback. After the rollback of the chunk the batch job should continue with the next item.

    If business Exception occurs how can we rollback the transcation?
    Last edited by arun4; Feb 14th, 2011 at 08:04 AM.

  5. #5
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    an exception from the item processor triggers a rollback for the chunk. But by default, it's going to fail the step execution. If you don't want this, configure the business exception as a skippable exception.

  6. #6

    Default Rollback the Transaction

    Thanks for your reply.

    My Batch Runs every day.
    On Month ends i will get even one billion records. All the one billion records can satisfy the business flow and throws the business exception.

    If I am using Skippable Exception then i need to set the skip limit. Upfront i cann't determine the skip limit.

  7. #7
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    you can specify your own skip policy if you don't want to deal with a skip limit. But if you face a lot of incorrect records, you should perhaps implement an item processor to filter them. This would lead to fewer rollbacks and make the job faster.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •