[英]Create metadata tables for Spring Batch in an embedded database
I'm using Spring Boot autoconfigured Spring Batch setup with @EnableBatchProcessing
. 我正在使用带有
@EnableBatchProcessing
Spring Boot自动配置的Spring Batch设置。 The problem is that it creates metadata tables in the main database and I don't want this behavior. 问题在于它在主数据库中创建了元数据表,我不希望出现这种情况。 I would like to save all the Spring Batch information to an embedded database.
我想将所有Spring Batch信息保存到嵌入式数据库中。
I've tried using the spring.batch.initialize-schema=embedded
property and adding H2 to the classpath, overriding DefaultBatchConfigurer
bean with the H2 data source, replacing JobRepository
and JobLauncher
beans but it constantly creates metadata tables in the main Oracle database. 我尝试使用
spring.batch.initialize-schema=embedded
属性并将H2添加到类路径中,用H2数据源覆盖DefaultBatchConfigurer
bean,替换JobRepository
和JobLauncher
bean,但它会在主Oracle数据库中不断创建元数据表。 I'm using Spring Batch 3.0.8 and Spring Boot 1.5.9. 我正在使用Spring Batch 3.0.8和Spring Boot 1.5.9。
Any help is appreciated, thank you! 任何帮助表示赞赏,谢谢!
UPDATE : Adding come configuration: 更新 :添加来配置:
H2 config: H2配置:
@Bean
public DataSource springBatchDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
return dataSource;
}
Oracle: 甲骨文:
@Bean
@Primary
public DataSource dataSource() throws SQLException {
PoolDataSourceImpl dataSource = new PoolDataSourceImpl();
dataSource.setConnectionFactoryClassName(environment.getRequiredProperty("db.driverClassName"));
dataSource.setURL(environment.getRequiredProperty("db.url"));
dataSource.setUser(environment.getRequiredProperty("db.username"));
dataSource.setPassword(environment.getRequiredProperty("db.password"));
dataSource.setFastConnectionFailoverEnabled(Boolean.valueOf(environment
.getRequiredProperty("db.fast.connect.failover.enabled")));
dataSource.setValidateConnectionOnBorrow(true);
dataSource.setSQLForValidateConnection("SELECT SYSDATE FROM DUAL");
dataSource.setONSConfiguration(environment.getRequiredProperty("db.ons.config"));
dataSource.setInitialPoolSize(Integer.valueOf(environment.getRequiredProperty("db.initial.pool.size")));
dataSource.setMinPoolSize(Integer.valueOf(environment.getRequiredProperty("db.min.pool.size")));
dataSource.setMaxPoolSize(Integer.valueOf(environment.getRequiredProperty("db.max.pool.size")));
dataSource.setAbandonedConnectionTimeout(120);
dataSource.setInactiveConnectionTimeout(360);
dataSource.setTimeToLiveConnectionTimeout(0);
return dataSource;
}
Batch configuration: 批量配置:
@Configuration
@EnableBatchProcessing
public class BatchConfigurer extends DefaultBatchConfigurer {
@Override
@Autowired
public void setDataSource(@Qualifier("springBatchDataSource") DataSource dataSource) {
super.setDataSource(dataSource);
}
}
and some properties related.. 和一些相关的属性
spring:
batch:
job.enabled: false
1.redefine the BasicBatchConfigurer 1.重新定义BasicBatchConfigurer
2.in spring-boot-batch-starter, you must create your own BatchDataSourceInitializer , so it will execute the init sql scripts 2.在spring-boot-batch-starter中,您必须创建自己的BatchDataSourceInitializer ,以便它将执行init sql脚本
@Configuration
public class MyBatchConfigurer {
@Bean
public BasicBatchConfigurer batchConfigurer(BatchProperties properties,
@Qualifier("springBatchDataSource") DataSource dataSource,
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
return new BasicBatchConfigurer(properties, dataSource,
transactionManagerCustomizers.getIfAvailable());
}
@Bean
public BatchDataSourceInitializer batchDataSourceInitializer(@Qualifier("springBatchDataSource") DataSource dataSource,
ResourceLoader resourceLoader, BatchProperties properties) {
return new BatchDataSourceInitializer(dataSource, resourceLoader,
properties);
}
}
You need to qualify your H2 datasource bean 您需要限定H2数据源bean
@Bean(name = "springBatchDataSource")
public DataSource springBatchDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
return dataSource;
}
I hope this help. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.