Use of Multiple Datasources in Spring Batch

Use of multiple DataSources in Spring Batch

AbstractBatchConfiguration tries to lookup BatchConfigurer in container first, if it is not found then tries to create it itself - this is where IllegalStateException is thrown where there is more than one DataSource bean in container.

The approach to solving the problem is to prevent from creation the DefaultBatchConfigurer bean in AbstractBatchConfiguration.
To do it we hint to create DefaultBatchConfigurer by Spring container using @Component annotation:

The configuration class where @EnableBatchProcessing is placed we can annotate with @ComponentScan that scan the package that contains the empty class that is derived from DefaultBatchConfigurer:

package batch_config;
...
@EnableBatchProcessing
@ComponentScan(basePackageClasses = MyBatchConfigurer.class)
public class MyBatchConfig {
...
}

the full code of that empty derived class is here:

package batch_config.components;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.stereotype.Component;
@Component
public class MyBatchConfigurer extends DefaultBatchConfigurer {
}

In this configuration the @Primary annotation works for DataSource bean as in the example below:

@Configuration
public class BatchTestDatabaseConfig {
@Bean
@Primary
public DataSource dataSource()
{
return .........;
}
}

This works for the Spring Batch version 3.0.3.RELEASE

The simplest solution to make @Primary annotation on DataSource work might be just adding @ComponentScan(basePackageClasses = DefaultBatchConfigurer.class) along with @EnableBatchProcessing annotation:

@Configuration
@EnableBatchProcessing
@ComponentScan(basePackageClasses = DefaultBatchConfigurer.class)
public class MyBatchConfig {

Using multiple datasources in springbatch (spring-boot-1.5.2.RELEASE) throwing exception on bootup

Use of multiple DataSources in Spring Batch

Check out this link.
Same question, and I solved my problem by that link.

Spring Boot Spring Batch : Multiple DataSource without Spring batch metadata table

According to your exception, it looks like Spring Batch is configured to use the Postgres data source for its metadata and it does not find the BATCH_JOB_INSTANCE table.

If you don't want to use meta-data tables, you can:

  • use the MapJobRepositoryFactoryBean to create an in-memory JobRepository
  • use an embedded database (H2, HSQL, etc) with the ResourcelessTransactionManager. Spring Boot will automatically create tables in the in-memory datasource for you (more details here: https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#howto-initialize-a-spring-batch-database).

Then you can configure an Oracle datasource for your reader and a Postgres datasource for your writer.

Nothing prevents from having multiple datasources in your Spring Batch app, you just need to configure which one to use for your business logic and which one to be used by Spring Batch for its internal mechanics.

There are similar questions that might help, I'm adding them here for reference:

  • Use of multiple DataSources in Spring Batch
  • How to java-configure separate datasources for spring batch data and business data? Should I even do it?


Related Topics



Leave a reply



Submit