Skip to content

ChunkOrientedStep does not restore the step execution when a chunk transaction is not committed #5594

Closed

Description

@AndreaBosmanWork

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

  1. Process three database records with chunk size one.
  2. Commit record one normally.
  3. In the second write, register a TransactionSynchronization whose beforeCommit throws an IllegalStateException.
  4. Verify STATUS_ROLLED_BACK in afterCompletion, and that only record one's business update remains committed.
  5. Check FAILED status 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 test

Expect 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

  1. changed the title [-]ChunkOrientedStep becomes UNKNOWN after a beforeCommit exception despite confirmed rollback[/-] [+]ChunkOrientedStep does not restore the step execution when a chunk transaction is not committed[/+] on Oct 16, 2026
  2. fmbenhassine commented on Oct 16, 2026

    @fmbenhassine
    Contributor

    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.

  3. added
    for: backport-to-6.0.x
    and removed
    status: waiting-for-triage
    on Oct 16, 2026
  4. added this to the 6.1.0-RC1 milestone on Oct 16, 2026
  5. added a commit that references this issue on Oct 16, 2026
    187594b
  6. added a commit that references this issue on Oct 16, 2026
    be85c66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: backport-to-6.0.xin: coretype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions