Spring boot Spring批处理未从setTablePrefix获取表前缀
我想为spring批处理使用一个不同的数据源,并创建了下面的配置类,并根据 我使用的是SpringBoot(2.2.6)和SpringBatch版本4.2.1.0Spring boot Spring批处理未从setTablePrefix获取表前缀,spring-boot,spring-batch,Spring Boot,Spring Batch,我想为spring批处理使用一个不同的数据源,并创建了下面的配置类,并根据 我使用的是SpringBoot(2.2.6)和SpringBatch版本4.2.1.0 @Configuration public class CustomBatchConfigurer extends DefaultBatchConfigurer { @Autowired @Qualifier("oracleDataSource") private DataSource da
@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,并且总是失败
找不到表格错误。
我需要使用上面的方法,因为我有两个不同的数据源,我需要SpringBatch来使用我的oracleDataSource
豆子
如果我禁用非oracleDataSource bean并将属性移动到application.properties,一切都会正常工作
请指导如何解决此问题。我在这里看到了一个类似的问题,用户在扩展DefaultBatchConfigurer类后抱怨同一个表未找到问题
调试spring批处理源代码后,我发现在创建jobExplorer时,表前缀被重置。有效的修复方法如下所示
@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();
}
}
我认为这应该在spring批处理文档中提到,因为jobExplorer是常用的,如果没有按如下所示正确重写,可以将表前缀重置回batch_uu
@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();
}
}
调试spring批处理源代码后,我发现在创建jobExplorer时,表前缀被重置。有效的修复方法如下所示
@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();
}
}
我认为这应该在spring批处理文档中提到,因为jobExplorer是常用的,如果没有按如下所示正确重写,可以将表前缀重置回batch_uu
@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();
}
}
在您的回答中,您说,
我发现在创建jobExplorer时,表前缀被重置[…]我认为这应该在spring批处理文档中提到:不,前缀没有重置或覆盖。JobRepository
和JobExplorer
是不同的组件,有两个不同的工厂bean来创建它们,因此您需要在这两个工厂bean上设置前缀,如您在回答中所示。在回答中,您说我发现在创建JobExplorer[…]时,表前缀被重置我认为这应该在spring批处理文档中提到:不,前缀没有重置或覆盖。JobRepository
和JobExplorer
是不同的组件,有两个不同的工厂bean来创建它们,因此您需要在这两个工厂bean上设置前缀,如您在回答中所示。