Results 1 to 3 of 3

Thread: Any ways to rollback transaction other than throwing exception?

  1. #1
    Join Date
    Apr 2009
    Posts
    6

    Default Any ways to rollback transaction other than throwing exception?

    Hello,

    In order to rollback the transaction used by each job execution, throwing exception in processor or writer is the only way?
    By throwing an exception, the execution will be marked as failure. But in my case, I'd like to successfully run job with intentional rollback.

    Usecase is like this:
    My batch process is regular style - read from DB, process it, then write to DB.
    I want to run the batch as if it run successfully but without persisting the result into database. The reason of not persisting data is testing the batch job in production with real data.
    By running job with no persisting, we obtain all logging, statistics information about the batch job without affecting production data. We observe those collected information, then once everything is ok, we flip the switch and start persisting data.

    So, rolling back transaction at the end of the execution is the pretty much best way for this scenario because I can even test the database constraints by writing the object in writer(then rollback later).

    I tried to retrieve the transaction used by trsnsactionTemplate in TaskletStep, but couldn't get it in writer or chunklistener classes.

    It would be nice if I can get the transaction(TransactionStatus) in listener and could able to call the setRollbackOnly().

  2. #2
    Join Date
    Apr 2008
    Location
    Philadelphia, US
    Posts
    198

    Arrow

    Quote Originally Posted by tadaya View Post
    The reason of not persisting data is testing the batch job in production with real data
    Can you run it against the production data from within a transactional integration test ( @TransactionConfiguration / @Transactional ) where 'defaultRollback' is already set to true?

    This way you won't be changing your jobs before running them in production.

    /Anatoly
    Humans are stateful and mutable beings that have no problems processing many things concurrently and share state with others + they are usually "coupled"

  3. #3
    Join Date
    Apr 2009
    Posts
    6

    Default

    ah, i see. I got what you meant, Anatoly, Thanks.

    We deploy app to the production env & dryrun the batch and collect the statistics.
    We keep the app running in dryrun for a couple of days/weeks to observe the statistics.
    When we think everything is ok, we change one of the value in the app, then start persisting data.


    So, I wrote a delegator class which wraps PlatformTransactionManager and controls commit/rollback.

    impl
    Code:
    public class RollbackTransactionManager implements PlatformTransactionManager {
    
        private PlatformTransactionManager transactionManager;
        private boolean doRollback;
    
        public void commit(TransactionStatus status) throws TransactionException {
            if (doRollback) {
                transactionManager.rollback(status);
            } else {
                transactionManager.commit(status);
            }
        }
         ......
    }
    job config file
    Code:
      <!-- provides a real txmanager to job repository -->
      <batch:job-repository id="jobRepository" transaction-manager="transactionManager"/>
    
      <batch:job id="job">
        <batch:step id="step">
          <!-- provides a wrapped txmanager -->
          <batch:tasklet transaction-manager="rollbackAwareTxManager">
            <batch:chunk reader="sampleReader" writer="sampleWriter"/>
          </batch:tasklet>
        </batch:step>
      </batch:job>
    
      <bean id="rollbackAwareTxManager" class="com.foobar.RollbackTransactionManager">
        <property name="transactionManager" ref="transactionManager" />
      </bean>

Posting Permissions

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