Announcement Announcement Module
No announcement yet.
JpaPagingItemReader Stale Object Exception Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JpaPagingItemReader Stale Object Exception

    We are having an issue with the JpaPagingItemReader. It seems what we are doing is a pretty simple case. We are using the reader to read x number of records; A processor to modifiy a field in that record; and using the JpaItemWriter to update the data.

    The issue occurs on the next read from the JpaPagingItemReader. The first thing it tries to do is a flush of its persistent context which is stale now.

    Am I using the Writer and Reader in a non normal fashion ( doesn't seem like it )? or am I missing some crucial property?

  • #2
    is the processing of a record causing updates to other records from the database that you haven't yet read/processed? That is what stale object smells like to me.

    Is there some way you can keep track of these changes and push them out at the end?


    • #3
      not that I know off. Its pretty much like this

      JpaPagingReader read Object

      Processor = object.setFoo( "bar");

      JpaWriter = object.merge();

      But the initial Item that is read exists in the Persistent Context of the reader, so before it does the next read it tries to do a flush()


      • #4
        Okay, I am not too familiar on how Spring Batch deals with JPA so I will have to defer on this one.


        • #5
          I was having the same issue.

          The issue is the JpaPagingItemReader.doPageRead(...) flushes the persistance context "before" the read instead of doing it after the read (like the javadoc sugests). So when you update an entity in the page number 1, and second call to this method triggers the stale state exception.

          As a work around I have created my own JpaPagingItemReader that does the flush "after" the read.

          I'll file a bug report too.


          • #6
            I see it is a quite old thread, however I had the same kind of problem using the JpaPagingItemReader which gave me some headache... For information, my current Spring Batch version is 2.1.9.RELEASE.

            As symptoms, I got duplicate entries on many-to-one relationships, or duplicate primary key error on one-to-one relationships, or stale exception when I tried to manage persistence by myself (on the other side of the relationship).

            I tried the same work around as the one explained by anshumanpurohit (ie. moving flush and clear instruction at the end of "doReadPage" method), and fortunately it works

            Did you fill a bug report on the Spring Batch Jira? The only problem with such a patch is that I don't know the side effects :/

            I hope my post would help some of those who have the same symptoms!


            • #7
              I was having similar problem, see:

              I think this bug is what is really behind the problem:

              While moving the flush() to after the doReadPage method will work in simple scenarios; it sounds like the reason they moved it to the beginning was to allow for lazy-loading of properties in the processor. If you're not lazy-loading properties it should be fine.

              Alternatively, I found another work-around that seems to work. Instead of having your JpaPagingItemReader return managed entities; have it return the ID of your entities. Then set up a composite ItemProcessor where the first processor accepts an ID, runs a JPA query to find by ID, and then returns the resulting entity. The second processor can remain as defined.