1

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?

Juru
  • 1,623
  • 17
  • 43
  • I'm thinking of the 'login' you are using to log to H2 db have a default schema which is not the one you are setting up for Spring Batch. – Nghia Do Sep 23 '16 at 18:52
  • 1
    look over here: http://stackoverflow.com/questions/5763747/h2-in-memory-database-table-not-found – Pete Sep 26 '16 at 23:18
  • I am facing same issue. Were you eventually able to find a solution @Juru ? – sutanu dalui Jul 18 '20 at 05:43
  • Yes look at the answer from pete. The db is discarded by default after each connection is closed, you need to change some property for another behaviour. – Juru Jul 19 '20 at 10:51

0 Answers0