【问题标题】:Spring-batch not getting the table prefix from setTablePrefixSpring-batch 未从 setTablePrefix 获取表前缀
【发布时间】: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:不,前缀没有被重置或覆盖。 JobRepositoryJobExplorer 是不同的组件,并且有两个不同的工厂 bean 来创建它们,因此您需要在两个工厂 bean 上设置前缀,如您在答案中所示。

标签: spring-boot spring-batch


【解决方案1】:

调试 spring-batch 源代码后,我发现创建 jobExplorer 时表前缀被重置。有效的修复方法如下。

我认为这应该在 spring-batch 文档中提到,因为 jobExplorer 是常用的,如果没有正确覆盖,可以将表前缀重置回 BATCH_,如下所示。

   
@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();
    }

    @Override
    protected JobLauncher createJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(createJobRepository());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

    @Override
    protected JobExplorer createJobExplorer() throws Exception {
        JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
        jobExplorerFactoryBean.setDataSource(this.dataSource);
        jobExplorerFactoryBean.setTablePrefix("MYDB.BATCH_");
        jobExplorerFactoryBean.afterPropertiesSet();
        return jobExplorerFactoryBean.getObject();
    }
    }


【讨论】:

    猜你喜欢
    • 2012-06-11
    • 2020-05-03
    • 1970-01-01
    • 2013-07-07
    • 2019-10-24
    • 2021-08-18
    • 2020-02-19
    • 2020-05-26
    • 1970-01-01
    相关资源
    最近更新 更多