Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

In Spring Batch, explain the relationship between a Job, a JobInstance, a JobExecution, a Step and a StepExecution, using a nightly 'import-transactions' job run with a date parameter as the example.

A Job is the definition of the work, such as import-transactions. A JobInstance is one logical run of that job for a set of identifying parameters, such as date=2026-11-02, and a JobExecution is one attempt to run that instance, so a failed night may have two or three executions. The job is made of Steps, each attempt of a step produces a StepExecution with its counts, an instance that completed cannot be run again with the same identifying parameters, and all of this is stored in the job repository.

Why this question is asked

These five names appear in every Spring Batch log, table and error message, and mixing them up is the fastest way to misread why a job will not start or why a restart did something unexpected. Interviewers ask this question to check that we understand the model Spring Batch uses to make jobs restartable and to stop the same day being processed twice. In this answer, we will take one nightly import-transactions job and walk through two nights of it, showing where each object appears, what it stores, and how they relate to each other in the job repository.

1A Quick Recap of What Spring Batch Is For

Spring Batch is a framework for jobs that process a lot of data without a user waiting: reading a file of transactions, loading them into a database, and producing a report. Such jobs run for minutes or hours, and they fail halfway sometimes. The framework's main promise is that we can find out exactly what happened in each run and restart a failed run from where it stopped.

To keep that promise it records everything in the job repository, a set of database tables. The five objects in the question are the vocabulary of those records. Our example job, import-transactions, has two steps: loadStep reads the bank's transaction file and writes rows to a table, and reportStep produces a summary. It runs every night with one parameter, the business date.

2Job and JobInstance: the Definition and One Logical Run

The Job is the definition we write in code: its name and the steps in order. There is one import-transactions Job, no matter how many nights it runs. A JobInstance is one logical run of that job, and it is identified by the job name plus its identifying JobParameters. Parameters are the inputs passed at launch, and by default they are identifying, meaning they decide which instance this is.

Java
JobParameters params = new JobParametersBuilder()
.addLocalDate("date", LocalDate.of(2026, 11, 2)) // identifying by default
.addString("requestedBy", "scheduler", false) // non-identifying
.toJobParameters();
jobLauncher.run(importTransactionsJob, params);
// JobInstance: import-transactions + {date=2026-11-02}

So import-transactions for 2 November is one JobInstance and for 3 November another. The requestedBy parameter is non-identifying: it is recorded with the execution, but it does not create a new instance. The reason for this design is the business meaning: the work for 2 November is one unit, whether it takes one attempt or three. In Spring Batch 5 and later, JobLauncher still works, and newer versions point toward JobOperator for launching; check your version.

3JobExecution: One Attempt at That Run

A JobExecution is one attempt to run a JobInstance. It records when the attempt started and ended, its status such as STARTED, COMPLETED or FAILED, and its exit status. Suppose on the night of 2 November the database goes away during loadStep. The first JobExecution ends FAILED. In the morning an operator restarts the job with the same date, which creates a second JobExecution of the same JobInstance, and this one completes.

Two nights of import-transactions
JobInstanceJobExecutionStatusSteps run
date=2026-11-02 (id 41)execution 101FAILEDloadStep failed at item 600,000
date=2026-11-02 (id 41)execution 102COMPLETEDloadStep resumed, reportStep ran
date=2026-11-03 (id 42)execution 103COMPLETEDloadStep, reportStep

Notice the relationships: instance 41 has two executions, instance 42 has one. That is the whole point of separating the two ideas. The instance says what work this is; the executions say how many times we tried and what happened each time.

4Step and StepExecution: the Pieces of Each Attempt

A Job is made of Steps, each an independent phase with its own logic: loadStep is chunk-oriented, reading and writing in chunks, and reportStep is a single task. Every time a step runs inside a JobExecution, Spring Batch creates a StepExecution. It records the step's status and counts: how many items were read, written, filtered and skipped, how many commits and rollbacks happened, and its start and end times.

Step throughHow the objects nest
  1. Job import-transactionsthe definition in code
  2. JobInstance date=2026-11-02one logical run, unique per identifying parameters
  3. JobExecution 101, 102attempts, each with status and times
  4. StepExecution loadStep, reportStepper attempt: read, write, skip, commit counts
  5. ExecutionContextsaved state, per job execution and per step execution

5 layers, from the top down. Step through them one at a time.

In execution 101, the loadStep StepExecution shows a read count of 600,000 and status FAILED, and reportStep has no StepExecution at all because it never started. In execution 102, the new loadStep StepExecution continues from the saved position, and reportStep gets its first StepExecution. A step that completed in an earlier execution is skipped on restart by default, which prevents doing finished work twice.

5The Job Repository and the ExecutionContext

All these objects are stored in the job repository tables: BATCH_JOB_INSTANCE, BATCH_JOB_EXECUTION, BATCH_JOB_EXECUTION_PARAMS, BATCH_STEP_EXECUTION and two context tables. Each JobExecution and each StepExecution also has an ExecutionContext, a small map of keys and values that Spring Batch saves at every commit. A file reader, for example, stores how many lines it has read. That stored position is what lets execution 102 continue at line 600,001 instead of starting again.

The repository also enforces the rule about completed instances. When we launch import-transactions with date=2026-11-03 again after execution 103 completed, Spring Batch finds that instance 42 is already COMPLETED and throws JobInstanceAlreadyCompleteException. This protects us from importing the same day's transactions twice, which in a payments system could mean double postings.

Java
jobLauncher.run(importTransactionsJob, paramsFor("2026-11-03"));
// throws JobInstanceAlreadyCompleteException: instance 42 already completed
jobLauncher.run(importTransactionsJob, paramsFor("2026-11-02"));
// also refused after execution 102 completed it

6Follow-up questions

How do you run the same job twice for the same date on purpose?

We make it a new JobInstance by adding another identifying parameter, so the identity changes. The usual tool is an incrementer such as RunIdIncrementer, which adds a run.id parameter that increases at each launch; Spring Boot applies the job's incrementer when it launches jobs. Another option is an explicit parameter like attempt=2 or rerunReason. We should only do this when the job is idempotent or the previous output has been reversed, because a deliberate re-run bypasses the protection that stops double processing.

What is the difference between status and exit status?

BatchStatus is the framework's own state, such as STARTED, COMPLETED, FAILED or STOPPED, and it drives restart rules. ExitStatus is a code that describes how the execution ended, and it can be customised, for example NO_DATA or VALIDATION_FAILED. Flows branch on exit status, while the repository uses batch status to decide whether an instance may be restarted.

Can I restart a JobInstance that ended STOPPED?

Yes. STOPPED and FAILED instances are restartable, and a restart creates a new JobExecution of the same instance. Only COMPLETED and ABANDONED instances are refused, the second because an operator marked the execution as not to be restarted.

7Conclusion

In this answer, we followed the nightly import-transactions job through two nights. The Job is the definition, the JobInstance is the job plus its identifying parameters such as date=2026-11-02, and each JobExecution is one attempt at that instance, so a failure and a restart give one instance with two executions. Each step attempt produces a StepExecution with its counts, ExecutionContexts carry the state needed for restart, and the job repository stores all of it and refuses to run a completed instance again.

What to say in the interviewAbout 80 seconds aloud

Open with

A Job is the definition, for example import-transactions with a load step and a report step. A JobInstance is one logical run of that job, identified by the job name plus the identifying parameters, so import-transactions for date 2026-11-02 is one instance and for the next day is another.

Then, if they want more

A JobExecution is one attempt at an instance. If the 2 November run fails halfway and the operator restarts it in the morning, that is still the same JobInstance, but now it has two JobExecutions, the first FAILED and the second COMPLETED.

Inside each execution, every step that runs gets a StepExecution, which records the read, write, filter and skip counts, commits, rollbacks and status. On restart, steps that already completed are skipped, and the failed step continues from the position saved in its ExecutionContext, which is saved at every commit.

All of this lives in the job repository tables, like BATCH_JOB_INSTANCE, BATCH_JOB_EXECUTION and BATCH_STEP_EXECUTION. Because of that record, once an instance completes, launching it again with the same identifying parameters throws JobInstanceAlreadyCompleteException, which protects us from importing the same day twice. If I really need to run it again, I add a new identifying parameter, usually with RunIdIncrementer.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Batch reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In Spring Batch, explain the relationship between a Job, a JobInstance, a JobExecution...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/c8c59644fd/