Results 1 to 6 of 6

Thread: ChunkTransactionListener?

  1. #1
    Join Date
    Jul 2005
    Location
    Austin, TX
    Posts
    137

    Default ChunkTransactionListener?

    Hi all,

    My use case is that I'm inserting a bunch of rows in a table in a chunk, and as part of the chunk's commit, I need to call a sproc that derives a bunch more (denormalized) data from the inserted rows.

    According to the ChunkListener interface, beforeChunk() is called after the chunk and its transaction start, and afterChunk() is called after the chunk is executed and the transaction is committed. Additionally, there's no Chunk/Step/JobExectution context passed into these methods. It's as though I need an interface called TransactionalChunkListener that looks like this:
    Code:
    public interface TransactionalChunkListener {
      public void beforeChunkBeforeTransactionStart(ChunkContext cx);
      public void beforeChunkAfterTransactionStart(ChunkContext cx);
      public void afterChunkBeforeTransactionCommit(ChunkContext cx); // this is the one I need
      public void afterChunkAfterTransactionCompletion(ChunkContext cx, boolean committed);
    }
    How do I achieve this in Spring Batch 2.1.x & later?

    -matthew

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

    Default

    can't you use the ItemWriteListener interface?

  3. #3
    Join Date
    Jul 2005
    Location
    Austin, TX
    Posts
    137

    Default

    Quote Originally Posted by arno View Post
    can't you use the ItemWriteListener interface?
    That might be just the ticket! I didn't see that one. I'll let you know.

    Thanks,
    Matthew

  4. #4
    Join Date
    Dec 2010
    Posts
    175

    Default

    Yes. ItemWriteListener should do the job but remember the behavior that I noticed was that onWrite() method gets called for each item written to the target because my commit count was set to 1.

  5. #5
    Join Date
    Jul 2005
    Location
    Austin, TX
    Posts
    137

    Default

    Quote Originally Posted by tiger.spring View Post
    Yes. ItemWriteListener should do the job but remember the behavior that I noticed was that onWrite() method gets called for each item written to the target because my commit count was set to 1.
    Noted. Thanks.

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

    Default

    I think ItemWriteListener covers the use case as I understand it. I don't see enough value in a new listener interface yet, and you have to be very careful with transaction boundaries because other components are going to try and push their actions to the last moment before a commit as well, and you can't easily assume that your callback will be the very last thing to happen. You best bet is to just join the transaction in ItemWriteListener.

Tags for this Thread

Posting Permissions

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