【发布时间】:2020-10-22 01:57:52
【问题描述】:
我想为 Spring Batch 使用不同的数据源,并创建了以下配置类,并根据 documentation 将我所需的数据源自动连接到此。
我正在使用 spring boot(2.2.6) 和 spring batch 版本 4.2.1.RELEASE
@Configuration
public class CustomBatchConfigurer extends DefaultBatchConfigurer {
@Autowired
@Qualifier("oracleDataSource")
private DataSource dataSource;
@Autowired
private PlatformTransactionManager transactionManager;
@Override
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource);
factory.setTransactionManager(transactionManager);
factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
factory.setTablePrefix("MYDB.BATCH_");
factory.setMaxVarCharLength(1000);
factory.afterPropertiesSet();
return factory.getObject();
}
}
但是当我用这个启动我的应用程序时,它永远不会应用 setTablePrefix 并且它总是 失败 找不到表错误。
我需要在上面使用,因为我有两个不同的数据源,我需要 spring 批处理来使用我的 oracleDataSource 豆子。
如果我禁用非 oracleDataSource bean 并将属性移动到 application.properties,一切正常。
请指导如何解决此问题。我在这里看到了一个类似的问题,用户在扩展 DefaultBatchConfigurer 类后抱怨找不到同一个表的问题 Spring batch tables in a different schema
【问题讨论】:
-
在你的回答中,你说
I found that the table prefix was getting reset while creating the jobExplorer [..] I think this should be mentioned in the spring-batch documentation:不,前缀没有被重置或覆盖。JobRepository和JobExplorer是不同的组件,并且有两个不同的工厂 bean 来创建它们,因此您需要在两个工厂 bean 上设置前缀,如您在答案中所示。