Repository navigation
Description
Bug description
With ChunkOrientedStep, a transaction synchronization throwing from beforeCommit after successful chunk processing and metadata updates leaves the job UNKNOWN and unable to restart, despite a confirmed rollback. The compatibility chunk engine records FAILED and restarts successfully with the same setup.
Environment
- Spring Batch 6.0.5, Spring Framework 7.0.9, Java 17
- PostgreSQL 17.11, PostgreSQL JDBC 42.7.13, Testcontainers 2.0.5
- JDBC JobRepository and business writes share a DataSource and DataSourceTransactionManager
- Default Batch execution-context serializer; no application code or custom serializer
The additional framework regression tests also reproduce the failures on upstream main at de91ffcaccf887c32f86b683d9430ff2329e4818 (6.1.0-SNAPSHOT).
Steps to reproduce
- Process three database records with chunk size one.
- Commit record one normally.
- In the second write, register a
TransactionSynchronizationwhosebeforeCommitthrows anIllegalStateException. - Verify
STATUS_ROLLED_BACKinafterCompletion, and that only record one's business update remains committed. - Check
FAILEDstatus and restart the same job instance to process records two and three.
The parameterized test compares chunk(1, transactionManager) with chunk(1).transactionManager(transactionManager). Compatibility passes; native fails at expected FAILED but was UNKNOWN.
Expected behavior
With a confirmed rollback and available metadata storage, the job is FAILED and can restart from the last committed checkpoint. This failure occurs before physical commit and observes a definite rollback outcome.
Actual behavior
The StepExecution version advances to 3 in memory. Rollback returns the database version to 2, but memory remains at 3. The final metadata save fails:
org.springframework.dao.OptimisticLockingFailureException:
Attempt to update step execution id=1 with wrong version (3), where current version is 2
The job becomes UNKNOWN and normal restart is blocked.
Minimal Complete Reproducible example
Standalone Maven/PostgreSQL reproducer.
With Java 17+ and Docker, run:
git clone --depth 1 --branch reproduce/chunk-rollback https://github.com/AndreaBosmanWork/spring-batch.git
cd spring-batch
./mvnw -B -f spring-batch-rollback-reproducer/pom.xml testExpect two invocations: compatibility passes and native fails at the status assertion. This branch uses the released, unpatched 6.0.5 dependency.
Proposed fix
A candidate on the separate fix branch follows TaskletStep's transaction-completion recovery. It restores the metadata snapshot after a confirmed rollback if metadata updating started, retains read/skip counts, and explicitly blocks restart for unknown transaction outcomes.
The new tests cover sequential/concurrent processing, saved reader and writer context, beforeCommit and stream-update failures, committed counters, restart, and uncertain commit outcomes. The full eight-module upstream Maven verification passes with 3,045 executed tests, zero failures/errors and 130 skipped tests reported by the existing suite. The isolated PostgreSQL/H2 verification and focused checks on both 6.0.5 and main also pass with the candidate.
Related history
#5199 and #5182 were resolved for 6.0.2, and their fixes are present in 6.0.5. This reproduction exercises a later failure after processing succeeds, during metadata updating or transaction completion.
Activity
[-]ChunkOrientedStep becomes UNKNOWN after a beforeCommit exception despite confirmed rollback[/-][+]ChunkOrientedStep does not restore the step execution when a chunk transaction is not committed[/+]fmbenhassine commentedon Oct 16, 2026
This is a valid issue. Thank you for reporting it and for providing an example!
The problem is broader than just an exception in "beforeCommit", so I edited the title accordingly.
Restore the step execution when a chunk transaction is not committed
Restore the step execution when a chunk transaction is not committed