FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Check only the last execution when restarting a job instance by jjh75607 · Pull Request #5503 · spring-projects/spring-batch · GitHub

Check only the last execution when restarting a job instance - #5503

Open
jjh75607 wants to merge 2 commits into
spring-projects:mainfrom
jjh75607:GH-5495
Open

Check only the last execution when restarting a job instance#5503
jjh75607 wants to merge 2 commits into
spring-projects:mainfrom
jjh75607:GH-5495

Conversation

jjh75607 commented Aug 27, 2026
edited
Loading

Copy link
Copy Markdown

Resolves #5495

When a job instance is restarted, TaskExecutorJobLauncher.createJobExecution reads the instance history twice: getJobExecutions(jobInstance) for the running / UNKNOWN / COMPLETED / ABANDONED checks, then getLastJobExecution(jobInstance) for the step checks and the execution context. Since SimpleJobRepository extends SimpleJobExplorer, the first call hydrates every past execution with its job instance, step executions and both execution contexts, so the number of statements per launch grows with the number of past executions. Before 6.0 this check lived in SimpleJobRepository and went through jobExecutionDao.findJobExecutions, which only loaded the job parameters per execution; moving it to the launcher in 90d8959 is where the slope went from one to eight statements per past execution.

This change drops the first read and runs the status checks against the last execution only. A new execution is only created once these checks pass on the previous one, so on the launcher's own path the last execution is the only one that can be running, UNKNOWN or COMPLETED.

Measured with the reproducer from #5495 (same instance restarted 12 times, statements counted with datasource-proxy on H2), on main before and after this change:

launch | before | after
     1 |     32 |    32
     2 |     50 |    41
     6 |     82 |    41
    12 |    130 |    41

What does change, and I want to be explicit about it: executions that are not the last one are no longer inspected. I built every combination of two past executions (8 statuses in each position) directly through JobRepository and compared main with this branch. The 35 cases where the older execution is FAILED or STOPPED, or where there is a single execution, behave identically, including the step status checks, preventRestart(), empty identifying parameters and carrying over the last execution context. The 6 cases where the older execution is running, COMPLETED, ABANDONED or UNKNOWN while a newer FAILED one exists were rejected on main and now proceed. Such a history cannot be produced through the launcher; it takes abandon on an older execution after the instance has already been restarted, a direct JobRepository.createJobExecution call, or two launches of the same instance racing past the check, which the existing code already only guards against probabilistically (see the comment above jobRepository.createJobExecution at the end of the method). If you would rather keep inspecting the full history, the alternative is a repository method that returns executions without hydrating them, which is an API addition I did not want to make on my own. Happy to go that way if you prefer.

Tests added to TaskExecutorJobOperatorTests for the last execution being running, being complete, and for a restart after several failed executions; these pass on main as well and are there to pin the behaviour. The spring-batch-core, spring-batch-integration and spring-batch-test suites pass. Also removes the now unused java.util.List import.

Update (2026-08-28): merged main to resolve a conflict with eec42cb, which changed the running-execution message inside the loop this PR removes. The improved message is kept (lastJobExecution instead of jobInstance), and the new TaskExecutorJobLauncherTests now stubs getLastJobExecution instead of getJobExecutions, which createJobExecution no longer calls. Its assertions are unchanged.

Signed-off-by: jjh75607 <jjh7560734@gmail.com>
Resolve conflict with eec42cb (Fix inaccurate exception message in
TaskExecutorJobLauncher). The message improvement is kept: the
running JobExecution, not the JobInstance, is reported. The new
TaskExecutorJobLauncherTests now stubs getLastJobExecution instead
of getJobExecutions, which createJobExecution no longer calls.

Signed-off-by: jjh75607 <jjh7560734@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Number of SQL statements per job launch grows with the number of past executions, and grew further in 6.0

1 participant


Back | FazBrowse Home | New Git URL