55

I just created a batch job using Spring Batch framework, but I don't have Database privileges to run CREATE SQL. When I try to run the batch job I hit the error while the framework tried to create TABLE_BATCH_INSTANCE. I try to disable the

<jdbc:initialize-database data-source="dataSource" enabled="false">    
 ...
</jdbc:initialize-database>

But after I tried I still hit the error

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist

Anyway can disable the SQL, I just want to test my reader writer and processor work properly.

12 Answers 12

70

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.

7
  • 1
    Just upgraded to Spring Boot 2.5.3 and had a problem with my H2 database + Flyway in test environment ... the manual creation of the BATCH tables was happening twice ... I had to set initialize-schema to never ... except the key seems to have now changed: spring.batch.jdbc.initialize-schema=never
    – Dan
    Aug 4, 2021 at 21:17
  • Good to know, I've tried to pinpoint when this changed, but can't find any reference to this new key anywhere in the migration guides or current documentation. I'm curious where/how you've learned it's changed? @Dan
    – BitfulByte
    Aug 5, 2021 at 22:17
  • 1
    now spring batch uses this property spring.batch.jdbc.initialize-schema=always Aug 24, 2022 at 7:36
  • 1
    Don't know about that but today I was using the old property in IntelliJ and it is showing that spring.batch.initialize-schema=always is deprecated . So just shared it here . Aug 24, 2022 at 12:47
  • 1
    Thanks @PrashantSingh I've found the source and updated the answer with additional info to warn future readers.
    – BitfulByte
    Aug 25, 2022 at 10:15
29

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

3
  • use MapJobRepositoryFactoryBean and ResourcelessTransactionManager disabled the SQL thanks
    – Einn Hann
    Oct 21, 2015 at 9:26
  • Sergio, just a simple question, can I remove/avoid to create those tables ? I am just using spring batch to get data from a table from DB
    – Sushi
    Jan 11, 2018 at 16:23
  • Sushi, that's what the first of the options I listed do ("If you can't create tables in the database then...")
    – Sergio
    Jan 12, 2018 at 10:40
14

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
1
  • 1
    Thank you a lot! I hit the next Error: JdbcSQLSyntaxErrorException: Table "BATCH_STEP_EXECUTION_CONTEXT" not found. I fixed my issue with this, and I'm using h2 db.
    – Yaroslav
    Jan 13, 2021 at 15:41
12

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)

1
  • this worked for me. replace spring.batch.initialize-schema=always with spring.batch.jdbc.initialize-schema=always Sep 2, 2022 at 5:07
8

To enable auto create spring batch data-schema simply add this line to your spring application.properties file :

spring.batch.initialize-schema=always

To understand more about Spring batch meta-data schema :

https://docs.spring.io/spring-batch/trunk/reference/html/metaDataSchema.html

1
  • I was trying to use postgreSQL to maintain the batch schema .This worked for me. THank you.!!! Jul 12, 2019 at 8:07
2

Seems silly, but someone can have the same problem.

I was receiving this error after drop all tables from a database. When I tried to start the Spring Batch, I received the error:

bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]

and:

Invalid object name 'BATCH_JOB_INSTANCE'

This happened to me because I drop the tables without restart the service. The service was started and receive the database metadata with the Batch tables on the database. After drop them and not restart the server, the Spring Batch thought that the tables still exists.

After restart the Spring Batch server and execute the batch again, the tables were created without error.

2

When running with Spring Boot:

Running with Spring Boot v1.5.14.RELEASE, Spring v4.3.18.RELEASE

This should be enough:

spring:
    batch:
        initializer:
            enabled: false

The initialize-schema did not work for this Spring boot version. After that I was able to copy the SQL scripts from the spring-core jar and change the table capitalization since this was my issue with the automatic table creation under Windows/Mac/Linux.

1
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">


    <!-- database -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/springbatch" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <!-- transaction manager -->
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <!-- create job-meta tables automatically -->
    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
        <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
    </jdbc:initialize-database>
</beans>

And make sure you are using compatible spring-jdbc -version with spring-batch. Most probably spring-jdbc-3.2.2.RELEASE.JAR compatible.

0

<jdbc:initialize-database/> tag is parsed by Spring using InitializeDatabaseBeanDefinitionParser. You can try debugging this class in your IDE to make sure what values are being picked up for enabled attribute. Also this value can be disabled by using JVM parameter -Dspring.batch.initializer.enabled=false

2
  • For the CREATE SQL it doesn't show the error so I assumed it is disabled. Just that when I re-run the batch job it prompt me the SQL error again but this time round is related to SELECT SQL.
    – Einn Hann
    Oct 21, 2015 at 6:42
  • Try running "SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = xxx and JOB_KEY =xxx" from database client with the credentials you are using for Spring Batch datasource configuration and see if you have any access issues.
    – Shailendra
    Oct 21, 2015 at 7:25
0

this works for me: Spring boot 2.0

  batch:
        initialize-schema: never
        initializer:
            enabled: false
0

Use the following setting as the suggested one above has been deprecated:

spring.batch.jdbc.initialize-schema=always

0

In your DataSourceConfig you should add this code. Once the app comes up it deletes any existing schema , or creates a new one .

private DataSource dataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL)
      .addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
      .addScript("classpath:org/springframework/batch/core/schema-h2.sql")
      .build();
}

If you are not able to do this , the schema scripts are present in your .m2 folder . Run the scripts manually. Location is inside spring-batch-core jar.

C:\Users\XXX.m2\repository\org\springframework\batch\spring-batch-core\4.3.7.

Split open this jar and find sql schema script you want.

  • org\springframework\batch\core\schema-h2.sql.
  • org\springframework\batch\core\schema-drop-h2.sql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.