I have set up a basic hello world project with spring batch while using a H2 database. You can find all the code here: https://github.com/GoossensMichael/springbatchpoc
A maven install is enough to build it and you can run the job with the following command:
java -cp "target/dependency-jars/*;target/greeter-1.0-SNAPSHOT.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/greeting.xml greetingJob
However I do not seem to be able to get it to work as H2 always complains about tables not existing or entries that were added to the database earlier can no longer be found.
Sep 23, 2016 8:15:12 PM org.springframework.batch.core.launch.support.CommandLineJobRunner start
SEVERE: Job Terminated in error: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is org.h2.jdbc.JdbcSQLException: Table "BATCH_JOB_INSTANCE" not found; SQ
L statement:
SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ? [42102-192]
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 org.h2.jdbc.JdbcSQLException: Table "BATCH_JOB_INSTANC
E" not found; SQL statement:
SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ? [42102-192]
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
In debug mode I have checked that the required tables for spring batch are added. But once the batch job itself tries to use the tables it is as if they never existed. When I would not use a jdbc:initialize-database springframework element and insert the spring batch schema by specifying an init script in the database connection url like this
<property name="url" value="jdbc:h2:mem:.:;INIT=RUNSCRIPT FROM 'resources/spring/batch/schema/schema-all.sql'"/>
Then the batch process would reach another point with similar effects. It would not find a db entry of a JOB EXECUTION which it has just created.
Changing the database to mysql makes it all work, but why doesn't it work with h2? Doesn't it support transactions or something? I would very much like an explanation for this or maybe a solution?