Repository navigation
Closed
Description
As of 6.0.3, it is impossible to skip invalid records with a JPA setup. The issue does not happen with a JDBC setup. This was initially discussed in #5303 and seems to be a bug in Spring Batch (to be confirmed). Here is a minimal example:
The sample is expected to insert some items in a table using a JpaItemWriter. When all items are valid, they are inserted correctly. If an item violates a database constraint and I try to skip it, the job fails with the following exception:
[main] ERROR org.springframework.batch.core.step.AbstractStep - COMMONS-LOGGING Encountered an error executing step myJobStep in job job
org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:803)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:757)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:149)
at org.springframework.transaction.support.TransactionOperations.executeWithoutResult(TransactionOperations.java:67)
at org.springframework.batch.core.step.item.ChunkOrientedStep.doExecute(ChunkOrientedStep.java:369)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:251)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:128)
at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:397)
at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:129)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:293)
at org.springframework.batch.core.launch.support.TaskExecutorJobLauncher$1.run(TaskExecutorJobLauncher.java:220)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:88)
at org.springframework.batch.core.launch.support.TaskExecutorJobLauncher.launchJobExecution(TaskExecutorJobLauncher.java:211)
at org.springframework.batch.core.launch.support.TaskExecutorJobLauncher.run(TaskExecutorJobLauncher.java:109)
at org.springframework.batch.core.launch.support.SimpleJobOperator.start(SimpleJobOperator.java:201)
at org.springframework.batch.core.launch.support.TaskExecutorJobOperator.start(TaskExecutorJobOperator.java:117)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:359)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:158)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:133)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:371)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:130)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:222)
at jdk.proxy2/jdk.proxy2.$Proxy60.start(Unknown Source)
at com.example.demo.DemoApplication.main(DemoApplication.java:47)
In this case, it is expected that the job succeeds and only the invalid item is skipped (this works as expected with JDBC).
Activity
quaff commentedon Apr 13, 2026
I think it's caused by JPA perform all inserts/updates at end of chunk which triggered by
entityManager.flush(), JDBC setup execute every insert/update on item write.MinChul-Son commentedon Apr 14, 2026
@fmbenhassine Hi,
I've analyzed this issue and would like to contribute a fix. Here's my proposed approach:
Root Cause
The
scan()method inChunkOrientedStepprocesses multiple items within a single transaction in scan mode. With JPA (Hibernate), whenentityManager.flush()fails for item A, Hibernate marks the entire transaction as rollback-only. When the next item B is subsequentlywritten in that same transaction, JPA immediately throws "Transaction silently rolled back because it has been marked as rollback-only" — which is not in the skip list, causing a
NonSkippableWriteExceptionand ultimately failing the step. JDBC does not have thisproblem because a failed SQL statement does not poison the entire transaction.
Proposed Fix
Change scan mode to process one item per transaction instead of all items in a single transaction:
ChunkTracker<O>— replacependingChunk: Chunk<O>with aLinkedList<O>queue, and addpollNextScanItem()/hasPendingScanItems()methods.processChunkSequentially()andprocessChunkConcurrently()scan mode blocks — calltracker.pollNextScanItem()to pull a single item per transaction, instead of passing the entire pending chunk toscan()at once.TransactionStatusparameter toscan()— explicitly callstatus.setRollbackOnly()when a skip occurs, ensuring consistent rollback behavior for both JDBC and JPA.This ensures JPA's rollback-only marking is isolated to a single item's transaction and does not affect subsequent items.I would love to be assigned to this issue and submit a PR. Would this approach be acceptable to the team?
fxpaquette commentedon Apr 14, 2026
I believe @MinChul-Son is correct and that the root cause (chunk scanning being done in a single transaction) may lead to non-JPA related issues.
I provided jdbc examples in #5361.
sjoerd-vogel commentedon Apr 14, 2026
I am not familiar enough to really comment on internal workings of spring batch. But @fxpaquette comment rings true to me as I have had complicated setups (interacting with multiple tables) attempting to migrate from jpa to jdbc as a proof of concept fail on me as well.
Fix skip policy with JPA ItemWriter in scan mode
Fix skip policy with JPA ItemWriter in scan mode
MinChul-Son commentedon Apr 15, 2026
#5379 is my purpose. Feel free to reviewing my stuff
Add integration test for #5377
Process each item in its own transaction in scan mode