Spring Batch Framework - Auto create Batch Table

Spring Batch uses the database to save metadata for its recover/retry functionality.

If you can't create tables in the database then you have to disable this behaviour

If you can create the batch metadata tables but not in runtime then you might create them manually


Since Spring Boot 2.5.0 use

# to always initialize the datasource:
spring.batch.jdbc.initialize-schema=always

# to only initialize an embedded datasource:
spring.batch.jdbc.initialize-schema=embedded

# to never initialize the datasource:
spring.batch.jdbc.initialize-schema=never

(spring.batch.initialize-schema is deprecated since 2.5.0 for removal in 2.7.0)


Spring Batch required following tables to run job

  • BATCH_JOB_EXECUTION
  • BATCH_JOB_EXECUTION_CONTEXT
  • BATCH_JOB_EXECUTION_PARAMS
  • BATCH_JOB_EXECUTION_SEQ
  • BATCH_JOB_INSTANCE
  • BATCH_JOB_SEQ
  • BATCH_STEP_EXECUTION
  • BATCH_STEP_EXECUTION_CONTEXT
  • BATCH_STEP_EXECUTION_SEQ

If you are using h2 db then it will create all required table by default

  • spring.h2.console.enabled=true
  • spring.datasource.url=jdbc:h2:mem:testdb
  • spring.datasource.driverClassName=org.h2.Driver
  • spring.datasource.username=sa
  • spring.datasource.password=
  • spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

When you start using Mysql or any other database you need to add follwing properties into application.properties

               spring.batch.initialize-schema=always

UPDATE: As of spring 2.5.0, you should use spring.batch.jdbc.initialize-schema instead. See source.


With Spring Boot 2.0 you probably need this: https://docs.spring.io/spring-boot/docs/2.0.0.M7/reference/htmlsingle/#howto-initialize-a-spring-batch-database

spring.batch.initialize-schema=always

By default it will only create the tables if you are using an embedded database.

Or

 spring.batch.initialize-schema=never

To permanently disable it.