当前位置: 首页 > 知识库问答 >
问题:

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

韶浩博
2023-03-14
@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个答案

郭易安
2023-03-14

spring-batch-core中没有db2zos模式,但是有DB2的模式。尝试在属性中显式指定:

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

看看有没有帮助。

更新

spring.batch.initializer.enabled=false
 类似资料:
  • 我们开发了一个Spring批处理应用程序,其中我们有两个流程。1.向前2.向后。我们只使用文件读/写,不涉及数据库。 > 正向场景:输入文件将包含22个字段的记录。通过执行序列号生成和添加一些填充字段等操作,将22个字段转换为32个字段。根据国家代码,输出将被分成最多3个。每个块将有250K条记录。(如果记录以百万为单位,则将为同一国家生成多个文件)。 800万张唱片需要36分钟。 800万记录将

  • 我无法在使用JPA的Spring启动应用程序中记录Spring批处理日志。这是属性文件配置(application.properties)。我想看看应用程序是否从日志中创建了Spring批处理表。 https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/resources/org/

  • 我的FlatFileItemWriter回调有一个奇怪的问题。我有一个自定义ItemWriter实现FlatFileFolterCallback和FlatFileHeaderCallback。因此,我在我的FlatFileItemWriter中设置页眉和页脚回调如下: ItemWriter Bean FlatFileItemWriter Bean 步进豆 我的writeFooter、writeHe

  • 我正在编写一个Spring批处理应用程序: null

  • 我定义了一个块,提交间隔为10,跳过限制为10。处理器类通过应用一些算术运算来操作字段。其中一条记录(比如第6条记录)在处理器类中发生异常。在此之后,再次处理1到5条记录,跳过第6条记录,处理7到10条记录,并将其写入XML(自定义XML编写器类)。由于处理器处理1-5条记录两次,因此预期字段值计算两次是错误的。您能否建议一种解决方案,让处理器只处理一次记录,只跳过失败的记录,并将处理后的记录写入