Spring Batch 不使用自定义数据源来创建表 [英] Spring Batch doesn't use custom datasource to create tables
问题描述
我正在研究 REST 服务(使用 Spring Boot),它运行批处理作业.我希望 Batch 只与嵌入式数据源(用于存储元数据),而默认数据源(在我的示例中为 Postgres)将用于存储业务实体.
I am working on REST-service (with Spring boot), which runs batch job. I want Batch to work only with embedded datasource (to store metadata), while default datasource (Postgres in my case) will be used to store buisness entities.
问题是 Batch 尝试在启动时在默认数据源中创建元数据表(如 batch_job_execution、batch_job_instance 等).
The problem is that Batch tries to create metadata tables (like batch_job_execution, batch_job_instance, etc.) in default datasource at startup.
以下是重现问题的示例配置:
Here is the sample configuration, which reproduces the problem:
批量配置
@Configuration
@EnableBatchProcessing
public class BatchConfiguration extends DefaultBatchConfigurer {
@Override
@Autowired
public void setDataSource(@Qualifier("batchDataSource") DataSource dataSource) {
super.setDataSource(dataSource);
}
}
数据源配置
@Configuration
public class DataSourceConfiguration {
@Bean
@Primary
public DataSource DataSource() {
final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(org.postgresql.Driver.class);
dataSource.setUrl("jdbc:postgresql://localhost:5432/test_batch");
dataSource.setUsername("user");
dataSource.setPassword("password");
return dataSource;
}
@Bean(name = "batchDataSource")
public DataSource batchDataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
}
}
使用此配置,我在微服务启动时在 Postgres 中获取批处理表,尽管之后似乎使用了嵌入式数据源,因为我在尝试时遇到 找不到表" H2 错误启动作业.
With this configuration I am getting batch tables in Postgres when microservice started, though aftewards, it seems, that embeded datasource is used, because I am getting "Table not found" error for H2 when trying to launch a job.
那么我应该如何正确编写配置以使 Batch 仅适用于嵌入式数据源?我不想要主数据源中的任何元数据(甚至是空表).
So how should I properly write configuration to make Batch work only with embedded datasource? I don't want any metadata (even empty tables) in main datasource.
更新:
正如 Michael Minella 所说,应该再添加一颗豆子:
As Michael Minella said, one more bean should be added:
@Configuration
@EnableBatchProcessing
public class BatchConfiguration extends DefaultBatchConfigurer {
@Override
@Autowired
public void setDataSource(@Qualifier("batchDataSource") DataSource dataSource) {
super.setDataSource(dataSource);
}
@Bean
public BatchDatabaseInitializer batchDatabaseInitializer(@Qualifier("batchDataSource") DataSource dataSource, ResourceLoader resourceLoader){
BatchDatabaseInitializer batchDatabaseInitializer = new BatchDatabaseInitializer(dataSource, resourceLoader, new BatchProperties());
return batchDatabaseInitializer;
}
}
推荐答案
使用 Spring Boot 不幸的是,BatchDataSourceInitializer
使用的 DataSource
与BatchConfigurer
.它只是在上下文中获取默认的 DataSource
.如果你配置你自己的BatchDataSourceInitializer
,Boot 不会启动,你可以定义直接使用哪个DataSource
.
Using Spring Boot the DataSource
that is used by the BatchDataSourceInitializer
is, unfortunately, not related to the BatchConfigurer
. It just grabs the default DataSource
in the context. If you configure your own BatchDataSourceInitializer
, the Boot one won't kick in and you can define which DataSource
is used directly.
这篇关于Spring Batch 不使用自定义数据源来创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!