我正在通过@EnableBatchProcessing
使用Spring Boot自动配置的Spring Batch设置。问题在于它在主数据库中创建了元数据表,我不希望出现这种情况。我想将所有Spring Batch信息保存到嵌入式数据库中。
我尝试使用spring.batch.initialize-schema=embedded
属性并将H2添加到类路径中,用H2数据源覆盖DefaultBatchConfigurer
bean,替换JobRepository
和JobLauncher
bean,但是在主Oracle数据库中不断创建元数据表。我正在使用Spring Batch 3.0.8和Spring Boot 1.5.9。
感谢您的帮助,谢谢!
更新:添加配置:
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;
}
批处理配置:
@Configuration
@EnableBatchProcessing
public class BatchConfigurer extends DefaultBatchConfigurer {
@Override
@Autowired
public void setDataSource(@Qualifier("springBatchDataSource") DataSource dataSource) {
super.setDataSource(dataSource);
}
}
和一些相关的属性..
spring:
batch:
job.enabled: false
答案 0 :(得分:1)
1。重新定义 BasicBatchConfigurer
2。在spring-boot-batch-starter中,您必须创建自己的 BatchDataSourceInitializer ,以便它将执行初始化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);
}
}
答案 1 :(得分:0)
您需要对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;
}
我希望有帮助。