Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Commit/rollback in spring batch

  1. #1
    Join Date
    Jul 2009
    Posts
    2

    Default Commit/rollback in spring batch

    I have mulpitle questions:

    1. When does the writer call "commit", is it after processing n times with commit-interval=n? If the reader reads 100 records and the commit-interval=10, then an exception occured on the 5th record...what happens to the first 4 records? Are they commited ?

    2. How is the commit actually done? does it commit nth records at the same time or one by one ?

    3. Does the framework call rollback when an exception has occured in the writer? Or do we have to explicitly make it rollback ?

    Thank You.

  2. #2
    Join Date
    Jun 2005
    Posts
    4,232

    Default

    There are a few things in your questions that are not 100% clear, or perhaps you have misunderstood what the role of the writer is.

    The transactions in a vanilla step are managed by the TaskletStep (which is what you get if you use the namespace and a <chunk/> configuration with a reader / writer etc.), including commit and rollback on exceptions. A <chunk/> in the namespace has a commit interval, and this is the number of items that are processed per transaction.

    An ItemWriter plays no role in transaction management.

    I do think that maybe the reference docs could be more explicit about this (raise a JIRA if you like).

  3. #3
    Join Date
    Jul 2009
    Posts
    2

    Default

    Thank you for explaining the commit interval Dave. I will refer to the docs for further details.

  4. #4
    Join Date
    Sep 2009
    Posts
    8

    Default

    So if you wanted to do a forced rollback on some condition, you could typically do that in ChunkListener.afterChunk()? Do you force a rollback by throwing an exception?

  5. #5
    Join Date
    Jun 2005
    Posts
    4,232

    Default

    Quote Originally Posted by Sagnet View Post
    So if you wanted to do a forced rollback on some condition, you could typically do that in ChunkListener.afterChunk()?
    In 2.0.x you could. In 2.1 the chunk listener was moved out of the transaction because ItemWriter already has an opportunity to veto a chunk, so there is some duplication.

    It doesn't really matter where you throw the exception, as long as it's in the transaction. If you use a custom Tasklet then inside execute() is fine, or if you use the standard item-oriented stuff then a reader, processor, writer or item listener also works.

    Do you force a rollback by throwing an exception?
    Yes. It's just normal Spring transactions. Nothing fancy really (except that any exception will cause a rollback, not just runtime exceptions).

  6. #6
    Join Date
    Sep 2009
    Posts
    8

    Default

    Quote Originally Posted by Dave Syer View Post
    It doesn't really matter where you throw the exception, as long as it's in the transaction.
    I need to throw the exception after all the items have been processed, but before the transaction is commited. In the latest version, where would the natural place to do that be? My commit interval is large enough to be confident that there will only be one chunk. I'm not using a custom tasklet, but a standard chunk tasklet.

  7. #7
    Join Date
    Jun 2005
    Posts
    4,232

    Default

    Sounds like you need to throw an exception in your writer?

  8. #8
    Join Date
    Sep 2009
    Posts
    8

    Default

    Quote Originally Posted by Dave Syer View Post
    Sounds like you need to throw an exception in your writer?
    I need to check a condition after all of the items have been through the processor. So I guess I have to make that check in the doWrite() method of the writer. (I'm using a HibernateItemWriter.)

  9. #9

    Default

    Quote Originally Posted by Dave Syer View Post
    In 2.0.x you could. In 2.1 the chunk listener was moved out of the transaction because ItemWriter already has an opportunity to veto a chunk, so there is some duplication.
    That sounds a bit problematic for me, because I would like to make use of chunk listener to do some preparation work for each chunk (in fact, writing and getting some 'meta-data' from DB for each chunk). If chunk listener is moved out from chunk txn, that will bring me a big headache ...

  10. #10
    Join Date
    Jun 2005
    Posts
    4,232

    Default

    The beforeChunk method was always inside the transaction as far as I remember, and it still is. Isn't that what you need? (We can always revert afterChunk() as well, if people rely on it - it's just we couldn't think of a use case where it would help to be inside a transaction where the writer couldn't do the same thing, whereas there are good reasons to want a callback after the transaction.)

Posting Permissions

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