Results 1 to 10 of 10

Thread: Making ItemReader threadsafe

  1. #1
    Join Date
    Jun 2010
    Posts
    15

    Default Making ItemReader threadsafe

    How do we make the StoredProcedureItemReader thread-safe?

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

    Default

    You can synchronize the read() method (e.g. by wrapping it in a delegator that does the synchronization). Remember that you will lose restartability, so best practice is to mark the step as not restartable and to be safe (and efficient) you can also set saveState=false on the reader.

  3. #3
    Join Date
    Jun 2010
    Posts
    15

    Default Multi thread fails...

    I am running the StoredProcedureItemReader in a multithreaded fasion. I get the below error
    Encountered an error executing the step
    org.springframework.dao.InvalidDataAccessResourceU sageException: Unexpected cursor position change.

    when I run in single threaded mode I dont have this issue....

  4. #4
    Join Date
    Jun 2010
    Posts
    15

    Default

    I am using the spring class StoredprocedureItemReader....How do i synchronise the read method then?

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

    Default

    Did you try my original suggestion: "wrapping it in a delegator that does the synchronization"?

  6. #6
    Join Date
    Jun 2010
    Posts
    15

    Default

    Im not sure how to do that...is there an example you can point to? Thanks a lot...

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

    Default

    How about this:

    Code:
    public SynchronizedItemReader<T> implements ItemReader<T> {
      private final ItemReader<T> delegate; 
      public SynchronizedItemReader(ItemReader<T> delegate) {
        this.delegate = delegate;
      }
      public synchronized T read () {
        return delegate.read();
      }
    }

  8. #8
    Join Date
    Jun 2010
    Posts
    15

    Default

    When I use the synchronisedItemReader, I get reader must be open to read exception.
    org.springframework.batch.item.ReaderNotOpenExcept ion: Reader must be open before it can be read.

  9. #9
    Join Date
    Jun 2010
    Posts
    15

    Default

    Im really stuck on a dead end..Could someone please review?

  10. #10
    Join Date
    Jun 2010
    Posts
    15

    Default

    I implemented the open and close in my itemreader and it worked. Thanks for your help Dave..

Posting Permissions

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