Skip to content

Fix TaskletStep to propagate checked exceptions when rollback is disabled#5322

Open
nikitanagar08 wants to merge 2 commits intospring-projects:mainfrom
nikitanagar08:fix-5316-taskletstep-checked-exceptions
Open

Fix TaskletStep to propagate checked exceptions when rollback is disabled#5322
nikitanagar08 wants to merge 2 commits intospring-projects:mainfrom
nikitanagar08:fix-5316-taskletstep-checked-exceptions

Conversation

@nikitanagar08
Copy link

Fixes #5316

Problem

With TaskletStep and a transaction attribute like PROPAGATION_NOT_SUPPORTED, a checked exception can be swallowed when rollback is disabled for that exception type. This causes the step loop to continue and re-execute the tasklet instead of failing.

Solution

Modified the exception handling in TaskletStep to:

  1. Always propagate exceptions (checked or unchecked) from tasklet.execute()
  2. Only mark rollback metadata (ROLLBACK_EXCEPTION_KEY and rollback count) when rollback is actually configured for the exception

Changes

  • TaskletStep.java: Modified exception handling to propagate all exceptions regardless of rollback configuration
  • Added regression tests for checked exceptions when rollbackOn returns false

Testing

Added tests to verify:

  • Checked exceptions are propagated when rollback is disabled (simulating PROPAGATION_NOT_SUPPORTED behavior)
  • Runtime exceptions are also properly propagated

Fixes spring-projects#5308

The previous fix for spring-projects#5217 addressed the OptimisticLockingFailureException
by checking for isStopping() status, but this did not completely eliminate
the race condition.

The issue occurs when:
1. The main thread calls jobOperator.stop()
2. The main thread tries to update step executions
3. The job thread is also updating step executions (after processing a chunk)
4. Both threads try to update simultaneously → OptimisticLockingFailureException

This commit:
- Catches OptimisticLockingFailureException when updating step executions
- Logs the exception at DEBUG level for diagnosis
- Allows the stop process to continue since the job will check the
  STOPPING status and stop anyway

Also improves the GracefulShutdownFunctionalTests.testStopJob test:
- Increases timeout from 1 second to 5 seconds to give jobs more time to stop
- Adds better logging to diagnose failures
- Adds detailed comments explaining the timing considerations

Signed-off-by: Nikita Nagar <permanayan84@gmail.com>
…bled

Fixes spring-projects#5316

Propagate checked exceptions from Tasklet.execute even when
transactionAttribute.rollbackOn(e) returns false. Only mark rollback
metadata (ROLLBACK_EXCEPTION_KEY and rollback count) when rollback is
actually configured for the exception.

Also add regression tests for PROPAGATION_NOT_SUPPORTED to ensure a
checked exception fails the step instead of being silently retried.

Signed-off-by: Nikita Nagar <permanayan84@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant