Spring批处理-BatchDatabaseInitializer:调用init方法失败

时间:2016-08-29 15:50:57

标签: java spring spring-batch

通过使用delow配置,我的spring批处理与内存数据库hsqldb一起工作正常。

@Bean
public ResourcelessTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

@Bean
public JobRepository jobRepository(ResourcelessTransactionManager transactionManager) throws Exception {
    MapJobRepositoryFactoryBean mapJobRepositoryFactoryBean = new MapJobRepositoryFactoryBean(transactionManager);
    mapJobRepositoryFactoryBean.setTransactionManager(transactionManager);
    return mapJobRepositoryFactoryBean.getObject();
}

@Bean
public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
    SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
    simpleJobLauncher.setJobRepository(jobRepository);
    return simpleJobLauncher;
}

但是当我连接DB2数据库批处理时,它会给我以下错误。

    class path resource [org/springframework/batch/core/schema-db2zos.sql] cannot be opened because it does not exist 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchDatabaseInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from class path resource [org/springframework/batch/core/schema-db2zos.sql]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/batch/core/schema-db2zos.sql] cannot be opened because it does not exist 

添加属性(spring.batch.schema = classpath:/org/springframework/batch/core/schema-db2.sql)后,frenzykryger提到异常。

Failed to execute SQL script statement #1 of resource class path resource [org/springframework/batch/core/schema-db2.sql]: CREATE TABLE BATCH_JOB_INSTANCE ( JOB_INSTANCE_ID BIGINT 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) ) 
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-551, SQLSTATE=42501, SQLERRMC=AK51602;CREATE TABLE;DSNDB04, DRIVER=4.19.26
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
xception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.deere.sample.main.Application.main(Application.java:22)
Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? order by JOB_INSTANCE_ID desc]; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=U90JDCPT.BATCH_JOB_INSTANCE, DRIVER=4.19.26
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=U90JDCPT.BATCH_JOB_INSTANCE, DRIVER=4.19.26
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)

添加属性(spring.batch.initializer.enabled = false)后获得以下异常。

Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.deere.sample.main.Application.main(Application.java:22)
Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? order by JOB_INSTANCE_ID desc]; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=U90JDCPT.BATCH_JOB_INSTANCE, DRIVER=4.19.26
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=U90JDCPT.BATCH_JOB_INSTANCE, DRIVER=4.19.26
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)
    at com.ibm.db2.jcc.am.kd.a(Unknown Source)

1 个答案:

答案 0 :(得分:0)

spring-batch-core中没有db2zos模式,但db2存在模式。尝试在属性中明确指定:

spring.batch.schema=classpath:/org/springframework/batch/core/schema-db2.sql

看看它是否有帮助。

更新

要完全禁用架构初始化,只需使用以下属性:

spring.batch.initializer.enabled=false

如需更多参考,请参阅org.springframework.boot.autoconfigure.batch.BatchProperties课程。它的字段映射到spring.batch.*属性。

在这种情况下,应该通过某种外部方式创建模式,或者您需要使用org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean来保持数据库中任何弹簧批处理状态。