Spring Batch Table Prefix when using Java Config
Solution 1
Simply add this line to any of your properties files registered on you batch configuration:
spring.batch.table-prefix= GFA.BATCH_
FYI, the prefix spring.batch
is mapped with org.springframework.boot.autoconfigure.batch.BatchProperties
provided with Spring boot. See source code on github.
Solution 2
You should define in java the BatchConfigurer and in this bean you define the spring batch tables prefix:
@Bean
public BatchConfigurer batchConfigurer() {
return new DefaultBatchConfigurer() {
@Autowired
PlatformTransactionManager platformTransactionManager;
@Override
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(secondaryDataSource(secondaryDataSourceProperties()));
factory.setTransactionManager(platformTransactionManager);
factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
factory.setTablePrefix(batchTablePrefix);
factory.setDatabaseType("POSTGRES");
factory.setMaxVarCharLength(maxVarcharSize);
return factory.getObject();
}
};
stefan.m
Currently I work with these technologies: AngularJS Java Wildfly MongoDB LDAP (Ping Directory) Camunda BPMN Android
Updated on July 17, 2022Comments
-
stefan.m almost 2 years
My Spring Batch repository (deployed on an Oracle database) lies in a different schema such that I need to prepend the schema name.
When using XML configuration, this would be easy to do:
<job-repository id="jobRepository" table-prefix="GFA.BATCH_" />
However, as I use Java Config, this turns out to be more tricky. The best solution I found is to have my Java Config class
extend DefaultBatchConfigurer
and override thecreateJobRepository()
method:@Configuration @EnableBatchProcessing public class BatchConfiguration extends DefaultBatchConfigurer{ @Autowired private DataSource dataSource; @Autowired private PlatformTransactionManager transactionManager; @Override protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.setTablePrefix("GFA.BATCH_"); factory.afterPropertiesSet(); return factory.getObject(); } ... }
Compared to the XML solution, that's pretty much code! And it's not too logical either - my first guess was to provide an
@Bean
method as follows:@Bean public JobRepository jobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.setTablePrefix("GFA.BATCH_"); factory.afterPropertiesSet(); return factory.getObject(); }
but this wouldn't work.
My question is: Is my solution optimal or is there a better one? I would prefer to define a Bean instead of having to override some method of some class which is not very intuitive... And obviously it would be even better if we could shorten the code to be somewhat close to the one-line code in the XML configuration.