我是Spring Batch的新手。我已经使用内存存储库配置了我的作业。但是,它似乎仍在使用数据库来保留作业元数据。我的弹簧批量配置是:
@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();
}
}
如何解决上述问题?
如果您使用的是Sprint启动应用程序中的简单属性。属性将解决问题spring.batch.initialize-schema=ALWAYS
对于非 Spring 引导设置:
在批处理配置中声明数据源 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();
}
初始化脚本可以在 spring-batch-core-xxx 中找到.jar org.springframework.batch.core
包下。
注意:我使用了内存数据库,但该解决方案也适用于其他数据库系统。
如果您使用的是 Spring 3.0,请删除@EnableBatchProcessing注释。
那些在 CentOS 中遇到同样问题的 MySql 数据库的人(大多数基于 Unix 的系统(。
表名在 Linux 中区分大小写。设置lower_case_table_names=1
解决了这个问题。
在此处查找官方文档
对于那些使用高于 spring-boot 2.5 的版本的用户,这在 application.properties 中工作
spring.batch.jdbc.initialize-schema = ALWAYS
这解决了我的情况:
spring.batch.jdbc.initialize-schema=ALWAYS