我使用H2数据库设置了一个带有spring批处理的基本hello world项目。您可以在此处找到所有代码:https://github.com/GoossensMichael/springbatchpoc
maven安装足以构建它,您可以使用以下命令运行该作业:
java -cp "target/dependency-jars/*;target/greeter-1.0-SNAPSHOT.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/greeting.xml greetingJob
但是我似乎无法让它工作,因为H2总是抱怨表不存在或者之前添加到数据库中的条目已经无法找到。
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)
在调试模式下,我已检查是否添加了弹簧批次所需的表格。但是一旦批处理作业本身尝试使用表格,就好像它们从未存在过一样。当我不使用jdbc:initialize-database springframework元素并通过在数据库连接URL中指定init脚本来插入spring批处理模式时
<property name="url" value="jdbc:h2:mem:.:;INIT=RUNSCRIPT FROM 'resources/spring/batch/schema/schema-all.sql'"/>
然后批处理将达到具有类似效果的另一点。它找不到它刚刚创建的JOB EXECUTION的db条目。
将数据库更改为mysql使一切正常,但为什么它不适用于h2?它不支持交易或其他什么吗?我非常想解释这个或者解决方案吗?