Amriou :
I am getting a similar problem. I have 2 datasources the primary and a second which should be used by spring batch. spring batch is creating the schema in the primary data source and trying to insert/update in the second here is my code:
The excetion: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: BATCH_JOB_INSTANCE in statement [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and 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();
}
}
And
@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);
}
}
Amriou :
This is what i did to make it work I just added a bean in which the second data source is injected.
@Bean
BatchConfigurer configurer(@Autowired @Qualifier("secondDatasource") DataSource dataSource) {
return new DefaultBatchConfigurer(dataSource);
}
but still the schema was not created and I used :
@Configuration
@Profile({"dev","prod"})
public class JobRepositorySchemaConfig {
private final String JOB_REPO_SCHEMA = "classpath:batch_repo_schema.sql";
@Autowired
@Qualifier("secondDatasource")
DataSource datasource;
@Autowired
WebApplicationContext webApplicationContext;
@PostConstruct
public void loadIfInMemory() throws Exception {
Resource resource = webApplicationContext.getResource("classpath:/org/springframework/batch/core/schema-drop-hsqldb.sql");
Resource resource2 = webApplicationContext.getResource("classpath:/org/springframework/batch/core/schema-hsqldb.sql");
ScriptUtils.executeSqlScript(datasource.getConnection(), resource);
ScriptUtils.executeSqlScript(datasource.getConnection(), resource2);
}
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=357494&siteId=1