java - Spring 批处理作业存储库表未在我配置的模式中创建
问题描述
我遇到了类似的问题。我有 2 个数据源,主数据源和第二个数据源应该由 Spring Batch 使用。spring batch 正在主数据源中创建模式并尝试在第二个中插入/更新这是我的代码:
异常:java.sql.SQLSyntaxErrorException:用户缺少权限或找不到对象:语句中的 BATCH_JOB_INSTANCE [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? 和 JOB_KEY = ?]
public class DataSourceConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties primaryDataSourceProperties() {
return new DataSourceProperties();
}
@Bean("secondDataSourceProperties")
@ConfigurationProperties("spring.second-datasource")
public DataSourceProperties secondDataSourceProperties() {
return new DataSourceProperties();
}
/**
* Create primary (default) DataSource.
*/
@Bean
@Primary
public DataSource primaryDataSource(@Autowired DataSourceProperties props) {
return props.initializeDataSourceBuilder().build();
}
/**
* Create second DataSource and named "secondDatasource".
*/
@Bean("secondDatasource")
public DataSource secondDataSource(@Autowired
@Qualifier("secondDataSourceProperties") DataSourceProperties props) {
return props.initializeDataSourceBuilder().build();
}
}
和
@Component
@Configuration
public class MyBatchConfigurer extends DefaultBatchConfigurer {
/**
* Initialize the BatchConfigurer to use the datasource of your choosing
*
* @param secondDatasource
*/
@Autowired
@Qualifier("secondDatasource")
DataSource datasource;
@Override
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(datasource);
factory.setTransactionManager(transactionManager());
factory.afterPropertiesSet();
return factory.getObject();
}
private PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(datasource);
}
}
解决方案
创建BatchConfigurer
.
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.batch.core.configuration.BatchConfigurationException;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
@Component
public class MyBatchConfigurer implements BatchConfigurer {
private PlatformTransactionManager transactionManager;
private JobRepository jobRepository;
private JobLauncher jobLauncher;
private JobExplorer jobExplorer;
@Autowired
@Qualifier("secondDatasource")
private DataSource dataSource;
@Override
public JobRepository getJobRepository() {
return jobRepository;
}
@Override
public PlatformTransactionManager getTransactionManager() {
return new DataSourceTransactionManager(datasource);
}
@Override
public JobLauncher getJobLauncher() {
return jobLauncher;
}
@Override
public JobExplorer getJobExplorer() {
return jobExplorer;
}
@PostConstruct
public void initialize() {
try {
this.jobRepository = createJobRepository();
this.jobExplorer = createJobExplorer();
this.jobLauncher = createJobLauncher();
} catch (Exception e) {
throw new BatchConfigurationException(e);
}
}
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(datasource);
factory.setTransactionManager(transactionManager());
factory.afterPropertiesSet();
return factory.getObject();
}
protected JobExplorer createJobExplorer() throws Exception {
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
jobExplorerFactoryBean.setDataSource(this.dataSource);
jobExplorerFactoryBean.afterPropertiesSet();
return jobExplorerFactoryBean.getObject();
}
}
推荐阅读
- c# - 如何在 Realm.Net 中设置“忽略”/仅运行时的 RealmObject 成员?
- python - 配置不当:“sql_server.pyodbc”在 CentOS7 中不是可用的数据库后端
- sql - 从 3m 返回数据,或下一个可用日期
- java - 哪个相当于 Java 的“isinstance”(Python)?
- python - Anaconda Gstreamer Gst packge 缺少“jpegenc”
- chart.js - Chart.js 如何为两个数据集创建图表 wirhout y 轴
- authentication - Joomla 登录被拒绝
- r - Shiny App:在模块中进行翻译并在它们之间浏览时更新语言选择
- html - 使用网格的问题 - 居中文本和使用间隙
- swift - 尝试在 Swift MapView 中进行自定义标注并更改标记颜色