Search code examples
springspring-bootjpaspring-batchh2

Spring Batch creates syntax errors in SQL statements for H2


I made an upgrade from Spring Boot & Batch from 1.5.9 to Spring Boot & Batch 2.5.6. Now I have an issue with the Spring Batch schema initialization. I am running H2 in a JUnit5 test class. First try is to initialzise via Spring Batch with:

spring:
  batch:
    jdbc:
      initialize-schema: "always"

It creates the Sequences but not any table. Looking into the trace it shows syntax error in the create table SQL: Syntax error in SQL statement (one example):

CREATE TABLE BATCH_JOB_INSTANCE ( JOB_INSTANCE_ID BIGINT IDENTITY[*] NOT NULL PRIMARY KEY , VERSION BIGINT , JOB_NAME VARCHAR(100) NOT NULL, JOB_KEY VARCHAR(32) NOT NULL, CONSTRAINT JOB_INST_UN UNIQUE (JOB_NAME, JOB_KEY) ) 

Indeed, checking qith H2 console or SQl linter it marks "IDENTITY[*]" as error and without that it would work in H2.

Second try as workaround fails, too. I put

spring:
  batch:
    jdbc:
      initialize-schema: "never"

and put all SQL as per Spring documentation https://docs.spring.io/spring-batch/docs/current/reference/html/schema-appendix.html#exampleDDLScripts into schema.sql. And all tables and sequences are created correctly. However, startup of Spring Boot fails with error:

Could not obtain sequence value; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Field "BATCH_JOB_SEQ.NEXTVAL" not found
Column "BATCH_JOB_SEQ.NEXTVAL" not found; SQL statement:
select BATCH_JOB_SEQ.nextval from dual [42122-204]

Of course the sequence has no column "nextval", since it is a sequence. Not even the workaround sequence tables in Spring Batch for databases without support for sequences have that column.

Database is correctly configured as H2 database (did not try another database).

Why does Spring Batch create everytime wrong SQL statements ? Can anyone point into the right direction ?


Solution

  • Solution was the H2 version. We upgraded also H2 to 2.0 but that version is not compatible with Spring Batch 4.3.4. Downgrade to H2 version 1.4.200 solved the issue.