【问题标题】:I am getting error Table 'test.batch_job_instance' doesn't exist我收到错误表'test.batch_job_instance'不存在
【发布时间】:2018-09-01 18:28:38
【问题描述】:

我是 Spring Batch 的新手。我已经用 inmemoryrepository 配置了我的工作。但是,它似乎仍在使用 DB 来持久化作业元数据。 我的spring批处理配置是:

@Configuration
public class BatchConfiguration {
    
    
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    
    @Autowired
    private JobBuilderFactory jobBuilder;
    
    @Bean
    public JobLauncher jobLauncher() throws Exception {
        SimpleJobLauncher job =new SimpleJobLauncher();
        job.setJobRepository(getJobRepo());
        job.afterPropertiesSet();
        return job;
    }
    
    
    @Bean
    public PlatformTransactionManager getTransactionManager() {
        return new ResourcelessTransactionManager();
    }

    @Bean
    public JobRepository getJobRepo() throws Exception {
        return new MapJobRepositoryFactoryBean(getTransactionManager()).getObject();
    }
    

    
    
    @Bean
    public Step step1(JdbcBatchItemWriter<Person> writer) throws Exception {
        return stepBuilderFactory.get("step1")
            .<Person, Person> chunk(10)
            .reader(reader())
            .processor(processor())
            .writer(writer).repository(getJobRepo())
            .build();
    }
    
     @Bean
    public Job job( @Qualifier("step1") Step step1) throws Exception {
        return jobBuilder.get("myJob").start(step1).repository(getJobRepo()).build();
    }

}

如何解决上述问题?

【问题讨论】:

    标签: spring-batch


    【解决方案1】:

    对于非 Spring Boot 设置:


    在批处理配置中声明数据源 bean 时会出现此错误。为了解决这个问题,我添加了一个嵌入式数据源,因为我不想在应用程序数据库中创建这些表:
    @Bean
    public DataSource mysqlDataSource() {
      // create your application datasource here
    }
    
    @Bean
    @Primary
    public DataSource batchEmbeddedDatasource() {
        // in memory datasource required by spring batch
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:schema-drop-h2.sql")
                .addScript("classpath:schema-h2.sql")
                .build();
    }
    

    初始化脚本可以在 org.springframework.batch.core 包下的 spring-batch-core-xxx.jar 中找到。
    注意我使用的是内存数据库,但解决方案也有效用于其他数据库系统。

    【讨论】:

      【解决方案2】:

      那些在 CentOS(大多数基于 Unix 的系统)中使用 MySql 数据库遇到同样问题的人。

      在 Linux 中表名区分大小写。设置lower_case_table_names=1 解决了这个问题。

      查找官方文档here

      【讨论】:

        【解决方案3】:

        如果您使用的是 Sprint 启动 application.properties 中的一个简单属性将解决问题 spring.batch.initialize-schema=总是

        【讨论】:

        • 以上方案适用于Spring Boot V2及以上版本。对于 2 之前的 Spring Boot 版本,您可以在您的测试类上使用以下两种(一种是删除模式,一种是在每次测试之前创建它)(在这种情况下,我使用内存数据库中的 h2 进行测试,否则,在jar文件夹中查找写入脚本): \n @Sql("classpath:org/springframework/batch/core/schema-drop-h2.sql") @Sql("classpath:org/springframework/batch /core/schema-h2.sql")
        • spring.batch.initialize-schema 在 Spring Boot 2.5 中已弃用。请改用spring.batch.jdbc.initialize-schema = ALWAYS
        猜你喜欢
        • 2018-06-10
        • 1970-01-01
        • 1970-01-01
        • 2011-01-04
        • 2019-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多