Dec 21st, 2010, 01:14 PM
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?
Dec 21st, 2010, 01:28 PM
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?
Dec 21st, 2010, 01:45 PM
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()
Dec 21st, 2010, 01:53 PM
Okay, I am not too familiar on how Spring Batch deals with JPA so I will have to defer on this one.
Apr 9th, 2012, 12:22 AM
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.
Apr 28th, 2013, 03:27 PM
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!