[[howto.batch]]
= Batch Applications

A number of questions often arise when people use Spring Batch from within a Spring Boot application.
This section addresses those questions.



[[howto.batch.specifying-a-data-source]]
== Specifying a Batch Data Source

Batch applications that store job details in an SQL database require a javadoc:javax.sql.DataSource[] bean.
A single javadoc:javax.sql.DataSource[] bean is required by default.
To have Spring Batch use a javadoc:javax.sql.DataSource[] other than the application’s main javadoc:javax.sql.DataSource[], declare a javadoc:javax.sql.DataSource[] bean, annotating its javadoc:org.springframework.context.annotation.Bean[format=annotation] method with javadoc:org.springframework.boot.batch.jdbc.autoconfigure.BatchDataSource[format=annotation].
If you do so and want two data sources (for example by retaining the main auto-configured javadoc:javax.sql.DataSource[]), set the `defaultCandidate` attribute of the javadoc:org.springframework.context.annotation.Bean[format=annotation] annotation to `false`.



[[howto.batch.specifying-a-transaction-manager]]
== Specifying a Batch Transaction Manager

Similar to xref:batch.adoc#howto.batch.specifying-a-data-source[], you can define a javadoc:org.springframework.transaction.PlatformTransactionManager[] for use in batch processing by annotating its javadoc:org.springframework.context.annotation.Bean[format=annotation] method with javadoc:org.springframework.boot.batch.autoconfigure.BatchTransactionManager[format=annotation].
If you do so and want two transaction managers (for example by retaining the auto-configured javadoc:org.springframework.transaction.PlatformTransactionManager[]), set the `defaultCandidate` attribute of the javadoc:org.springframework.context.annotation.Bean[format=annotation] annotation to `false`.



[[howto.batch.specifying-a-task-executor]]
== Specifying a Batch Task Executor

Similar to xref:batch.adoc#howto.batch.specifying-a-data-source[], you can define a javadoc:org.springframework.core.task.TaskExecutor[] for use in batch processing by annotating its javadoc:org.springframework.context.annotation.Bean[format=annotation] method with javadoc:org.springframework.boot.batch.autoconfigure.BatchTaskExecutor[format=annotation].
If you do so and want two task executors (for example by retaining the auto-configured javadoc:org.springframework.core.task.TaskExecutor[]), set the `defaultCandidate` attribute of the javadoc:org.springframework.context.annotation.Bean[format=annotation] annotation to `false`.



[[howto.batch.running-from-the-command-line]]
== Running From the Command Line

Spring Boot converts any command line argument starting with `--` to a property to add to the javadoc:org.springframework.core.env.Environment[], see xref:reference:features/external-config.adoc#features.external-config.command-line-args[accessing command line properties].
This should not be used to pass arguments to batch jobs.
To specify batch arguments on the command line, use the regular format (that is without `--`), as shown in the following example:

[source,shell]
----
$ java -jar myapp.jar someParameter=someValue anotherParameter=anotherValue
----

If you specify a property of the javadoc:org.springframework.core.env.Environment[] on the command line, it is ignored by the job.
Consider the following command:

[source,shell]
----
$ java -jar myapp.jar --server.port=7070 someParameter=someValue
----

This provides only one argument to the batch job: `someParameter=someValue`.



[[howto.batch.restarting-a-failed-job]]
== Restarting a Stopped or Failed Job

To restart a failed javadoc:org.springframework.batch.core.job.Job[], all parameters (identifying and non-identifying) must be re-specified on the command line.
Non-identifying parameters are *not* copied from the previous execution.
This allows them to be modified or removed.

NOTE: When you're using a custom javadoc:org.springframework.batch.core.job.parameters.JobParametersIncrementer[], you have to gather all parameters managed by the incrementer to restart a failed execution.


