JpaPagingItemReader transaction issue : StaleObjectStateExceptionSpring Batch Transaction commit outside commit intervalSpring Batch: Ignore any line that does not match the specified patternspring batch and JTASpring Data JPA Persisting Entity with a nested Composite Primary Key entity with itself a nested entity which is detached at persist timeI am getting java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based while doing bulk updateTrying to split files using SystemCommandTasklet - Execution of system command did not finish within the timeoutFailed to convert property value of type 'java.util.Collections$UnmodifiableMap' to required type 'org.springframework.batch.item.ExecutionContext'Optimistic locking when update step execution in Spring BatchSpring boot batch with HibernateCursorItemReader not workingSpring batch reader complain about resource

Telephone number in spoken words

Is there any way I will not use l'Hôpital's rule here?

Help, I cannot decide when to start the story

Go to last file in vim

Setting up a Mathematical Institute of Refereeing?

Sum Square Difference, which way is more Pythonic?

Running code generated in realtime in JavaScript with eval()

Is there a fallacy about "appeal to 'big words'"?

What is the prop for Thor's hammer (Mjölnir) made of?

Why aren't rainbows blurred-out into nothing after they are produced?

What can Amex do if I cancel their card after using the sign up bonus miles?

Do I have to cite common CS algorithms?

Word for an event that will likely never happen again

onomatopoeia for cluelessness

Why do my bicycle brakes get worse and feel more 'squishy" over time?

What unique challenges/limitations will I face if I start a career as a pilot at 45 years old?

How to not forget things?

Attacking the Hydra

Is Thieves' Cant a language?

How can I find an old paper when the usual methods fail?

Match 4 columns and replace 1 in 2 files

Stop email from sending using AMPscript

Did Pope Urban II issue the papal bull "terra nullius" in 1095?

Cycle of actions and voice signals on a multipitch climb



JpaPagingItemReader transaction issue : StaleObjectStateException


Spring Batch Transaction commit outside commit intervalSpring Batch: Ignore any line that does not match the specified patternspring batch and JTASpring Data JPA Persisting Entity with a nested Composite Primary Key entity with itself a nested entity which is detached at persist timeI am getting java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based while doing bulk updateTrying to split files using SystemCommandTasklet - Execution of system command did not finish within the timeoutFailed to convert property value of type 'java.util.Collections$UnmodifiableMap' to required type 'org.springframework.batch.item.ExecutionContext'Optimistic locking when update step execution in Spring BatchSpring boot batch with HibernateCursorItemReader not workingSpring batch reader complain about resource






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Below is my spring batch chunk configuration



  1. Reader - Reading entity using JpaPagingItemReader

  2. Processor - Processing entity returned by reader and updating some fields in same entity based on different conditions

  3. Writer - simple logic to call entitymanager.merge(entity) to merge changes done in processor

<batch:step id="matchUploadedTradesWithUbsTrades" next="updateJobStatus">
<batch:tasklet>
<batch:chunk
reader="matchingDatasetReader"
processor="matchingDatasetProcessor"
writer="matchingDatasetWriter"
commit-interval="10"
/>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="stepExecutionListener" />
</batch:listeners>
</batch:step>

<bean id="matchingDatasetReader" class="com.batch.reader.MatchingJobDatasetReader" scope="job">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
<property name="queryString" value="# T(com.batch.jpa.entity.Entity).QUERY_FIND_RECORDDS"/>
<property name="pageSize" value="10" />
</bean>

<bean id="matchingDatasetProcessor" class="com.batch.processor.MatchingJobDatasetProcessor" scope="job"/>

<bean id="matchingDatasetWriter" class="com.batch.writer.MatchingJobDatasetWriter" scope="job"/>


I am facing issue with JpaPagingItemReader with below two scenarios.



Scenario-1 – I have set commit-interval (10) in batch configuration less than total number of records(15 records) in entity and call to entitymanager.merge(entity) is commented in writer
commit-interval = 10
Total records = 15



Observation : Records are getting updated in database. Reason for this is when keep commit-interval less than no of records, JpaPagingItemReader calls entitymanager.flush before reading next batch which commits changes to database without calling entitymanager.merge method.



Scenario-2 – I have set commit-interval in batch configuration less than total number of records in client trades and call update method in writer
commit-interval = 10
Total records = 15



Observation : I am getting StaleObjectStateException because entity is already updated from writer by calling entitymanager.merge and Reader will try to flush changes done by writer.



javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.batch.jpa.entity.Client#29657]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1785)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1705)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1338)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
at com.sun.proxy.$Proxy76.flush(Unknown Source)
at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:199)
at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:108)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy85.read(Unknown Source)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.batch.jpa.entity.Client#29657]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2541)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3285)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:158)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1335)
... 61 more



I tried setting custom chunk Policy also but same behavior I have observed.



Can someone help me to handle both scenarios?










share|improve this question
























  • we can't know upfront the total number of records (runtime) and set the commit-interval accordingly (configuration time), so for me, both scenarios should work without any issue. Otherwise there is a bug in Spring Batch. Row was updated or deleted by another transaction: Does your processor use a DAO or entity manager participating in a transaction other than the one driven by Spring Batch?

    – Mahmoud Ben Hassine
    Mar 28 at 15:22











  • No processor is not using any separate entity manager other than managed by spring. Code which processor invokes to update database is within spring batch context only.

    – Sandeep Thaker
    Apr 2 at 9:19











  • Please share your processor's code as well as your transaction manger configuration.

    – Mahmoud Ben Hassine
    Apr 2 at 12:17

















0















Below is my spring batch chunk configuration



  1. Reader - Reading entity using JpaPagingItemReader

  2. Processor - Processing entity returned by reader and updating some fields in same entity based on different conditions

  3. Writer - simple logic to call entitymanager.merge(entity) to merge changes done in processor

<batch:step id="matchUploadedTradesWithUbsTrades" next="updateJobStatus">
<batch:tasklet>
<batch:chunk
reader="matchingDatasetReader"
processor="matchingDatasetProcessor"
writer="matchingDatasetWriter"
commit-interval="10"
/>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="stepExecutionListener" />
</batch:listeners>
</batch:step>

<bean id="matchingDatasetReader" class="com.batch.reader.MatchingJobDatasetReader" scope="job">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
<property name="queryString" value="# T(com.batch.jpa.entity.Entity).QUERY_FIND_RECORDDS"/>
<property name="pageSize" value="10" />
</bean>

<bean id="matchingDatasetProcessor" class="com.batch.processor.MatchingJobDatasetProcessor" scope="job"/>

<bean id="matchingDatasetWriter" class="com.batch.writer.MatchingJobDatasetWriter" scope="job"/>


I am facing issue with JpaPagingItemReader with below two scenarios.



Scenario-1 – I have set commit-interval (10) in batch configuration less than total number of records(15 records) in entity and call to entitymanager.merge(entity) is commented in writer
commit-interval = 10
Total records = 15



Observation : Records are getting updated in database. Reason for this is when keep commit-interval less than no of records, JpaPagingItemReader calls entitymanager.flush before reading next batch which commits changes to database without calling entitymanager.merge method.



Scenario-2 – I have set commit-interval in batch configuration less than total number of records in client trades and call update method in writer
commit-interval = 10
Total records = 15



Observation : I am getting StaleObjectStateException because entity is already updated from writer by calling entitymanager.merge and Reader will try to flush changes done by writer.



javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.batch.jpa.entity.Client#29657]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1785)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1705)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1338)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
at com.sun.proxy.$Proxy76.flush(Unknown Source)
at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:199)
at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:108)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy85.read(Unknown Source)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.batch.jpa.entity.Client#29657]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2541)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3285)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:158)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1335)
... 61 more



I tried setting custom chunk Policy also but same behavior I have observed.



Can someone help me to handle both scenarios?










share|improve this question
























  • we can't know upfront the total number of records (runtime) and set the commit-interval accordingly (configuration time), so for me, both scenarios should work without any issue. Otherwise there is a bug in Spring Batch. Row was updated or deleted by another transaction: Does your processor use a DAO or entity manager participating in a transaction other than the one driven by Spring Batch?

    – Mahmoud Ben Hassine
    Mar 28 at 15:22











  • No processor is not using any separate entity manager other than managed by spring. Code which processor invokes to update database is within spring batch context only.

    – Sandeep Thaker
    Apr 2 at 9:19











  • Please share your processor's code as well as your transaction manger configuration.

    – Mahmoud Ben Hassine
    Apr 2 at 12:17













0












0








0








Below is my spring batch chunk configuration



  1. Reader - Reading entity using JpaPagingItemReader

  2. Processor - Processing entity returned by reader and updating some fields in same entity based on different conditions

  3. Writer - simple logic to call entitymanager.merge(entity) to merge changes done in processor

<batch:step id="matchUploadedTradesWithUbsTrades" next="updateJobStatus">
<batch:tasklet>
<batch:chunk
reader="matchingDatasetReader"
processor="matchingDatasetProcessor"
writer="matchingDatasetWriter"
commit-interval="10"
/>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="stepExecutionListener" />
</batch:listeners>
</batch:step>

<bean id="matchingDatasetReader" class="com.batch.reader.MatchingJobDatasetReader" scope="job">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
<property name="queryString" value="# T(com.batch.jpa.entity.Entity).QUERY_FIND_RECORDDS"/>
<property name="pageSize" value="10" />
</bean>

<bean id="matchingDatasetProcessor" class="com.batch.processor.MatchingJobDatasetProcessor" scope="job"/>

<bean id="matchingDatasetWriter" class="com.batch.writer.MatchingJobDatasetWriter" scope="job"/>


I am facing issue with JpaPagingItemReader with below two scenarios.



Scenario-1 – I have set commit-interval (10) in batch configuration less than total number of records(15 records) in entity and call to entitymanager.merge(entity) is commented in writer
commit-interval = 10
Total records = 15



Observation : Records are getting updated in database. Reason for this is when keep commit-interval less than no of records, JpaPagingItemReader calls entitymanager.flush before reading next batch which commits changes to database without calling entitymanager.merge method.



Scenario-2 – I have set commit-interval in batch configuration less than total number of records in client trades and call update method in writer
commit-interval = 10
Total records = 15



Observation : I am getting StaleObjectStateException because entity is already updated from writer by calling entitymanager.merge and Reader will try to flush changes done by writer.



javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.batch.jpa.entity.Client#29657]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1785)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1705)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1338)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
at com.sun.proxy.$Proxy76.flush(Unknown Source)
at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:199)
at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:108)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy85.read(Unknown Source)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.batch.jpa.entity.Client#29657]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2541)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3285)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:158)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1335)
... 61 more



I tried setting custom chunk Policy also but same behavior I have observed.



Can someone help me to handle both scenarios?










share|improve this question














Below is my spring batch chunk configuration



  1. Reader - Reading entity using JpaPagingItemReader

  2. Processor - Processing entity returned by reader and updating some fields in same entity based on different conditions

  3. Writer - simple logic to call entitymanager.merge(entity) to merge changes done in processor

<batch:step id="matchUploadedTradesWithUbsTrades" next="updateJobStatus">
<batch:tasklet>
<batch:chunk
reader="matchingDatasetReader"
processor="matchingDatasetProcessor"
writer="matchingDatasetWriter"
commit-interval="10"
/>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="stepExecutionListener" />
</batch:listeners>
</batch:step>

<bean id="matchingDatasetReader" class="com.batch.reader.MatchingJobDatasetReader" scope="job">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
<property name="queryString" value="# T(com.batch.jpa.entity.Entity).QUERY_FIND_RECORDDS"/>
<property name="pageSize" value="10" />
</bean>

<bean id="matchingDatasetProcessor" class="com.batch.processor.MatchingJobDatasetProcessor" scope="job"/>

<bean id="matchingDatasetWriter" class="com.batch.writer.MatchingJobDatasetWriter" scope="job"/>


I am facing issue with JpaPagingItemReader with below two scenarios.



Scenario-1 – I have set commit-interval (10) in batch configuration less than total number of records(15 records) in entity and call to entitymanager.merge(entity) is commented in writer
commit-interval = 10
Total records = 15



Observation : Records are getting updated in database. Reason for this is when keep commit-interval less than no of records, JpaPagingItemReader calls entitymanager.flush before reading next batch which commits changes to database without calling entitymanager.merge method.



Scenario-2 – I have set commit-interval in batch configuration less than total number of records in client trades and call update method in writer
commit-interval = 10
Total records = 15



Observation : I am getting StaleObjectStateException because entity is already updated from writer by calling entitymanager.merge and Reader will try to flush changes done by writer.



javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.batch.jpa.entity.Client#29657]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1785)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1705)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1338)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
at com.sun.proxy.$Proxy76.flush(Unknown Source)
at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:199)
at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:108)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy85.read(Unknown Source)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:272)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.batch.jpa.entity.Client#29657]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2541)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3285)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:158)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1335)
... 61 more



I tried setting custom chunk Policy also but same behavior I have observed.



Can someone help me to handle both scenarios?







java spring jpa spring-batch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 11:39









Sandeep ThakerSandeep Thaker

187 bronze badges




187 bronze badges















  • we can't know upfront the total number of records (runtime) and set the commit-interval accordingly (configuration time), so for me, both scenarios should work without any issue. Otherwise there is a bug in Spring Batch. Row was updated or deleted by another transaction: Does your processor use a DAO or entity manager participating in a transaction other than the one driven by Spring Batch?

    – Mahmoud Ben Hassine
    Mar 28 at 15:22











  • No processor is not using any separate entity manager other than managed by spring. Code which processor invokes to update database is within spring batch context only.

    – Sandeep Thaker
    Apr 2 at 9:19











  • Please share your processor's code as well as your transaction manger configuration.

    – Mahmoud Ben Hassine
    Apr 2 at 12:17

















  • we can't know upfront the total number of records (runtime) and set the commit-interval accordingly (configuration time), so for me, both scenarios should work without any issue. Otherwise there is a bug in Spring Batch. Row was updated or deleted by another transaction: Does your processor use a DAO or entity manager participating in a transaction other than the one driven by Spring Batch?

    – Mahmoud Ben Hassine
    Mar 28 at 15:22











  • No processor is not using any separate entity manager other than managed by spring. Code which processor invokes to update database is within spring batch context only.

    – Sandeep Thaker
    Apr 2 at 9:19











  • Please share your processor's code as well as your transaction manger configuration.

    – Mahmoud Ben Hassine
    Apr 2 at 12:17
















we can't know upfront the total number of records (runtime) and set the commit-interval accordingly (configuration time), so for me, both scenarios should work without any issue. Otherwise there is a bug in Spring Batch. Row was updated or deleted by another transaction: Does your processor use a DAO or entity manager participating in a transaction other than the one driven by Spring Batch?

– Mahmoud Ben Hassine
Mar 28 at 15:22





we can't know upfront the total number of records (runtime) and set the commit-interval accordingly (configuration time), so for me, both scenarios should work without any issue. Otherwise there is a bug in Spring Batch. Row was updated or deleted by another transaction: Does your processor use a DAO or entity manager participating in a transaction other than the one driven by Spring Batch?

– Mahmoud Ben Hassine
Mar 28 at 15:22













No processor is not using any separate entity manager other than managed by spring. Code which processor invokes to update database is within spring batch context only.

– Sandeep Thaker
Apr 2 at 9:19





No processor is not using any separate entity manager other than managed by spring. Code which processor invokes to update database is within spring batch context only.

– Sandeep Thaker
Apr 2 at 9:19













Please share your processor's code as well as your transaction manger configuration.

– Mahmoud Ben Hassine
Apr 2 at 12:17





Please share your processor's code as well as your transaction manger configuration.

– Mahmoud Ben Hassine
Apr 2 at 12:17












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376309%2fjpapagingitemreader-transaction-issue-staleobjectstateexception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376309%2fjpapagingitemreader-transaction-issue-staleobjectstateexception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해