title: Spring Boot Batch dynamic multiple dataSource with JPA
body: <p>Project based on Spring boot and batch. I need to use h2 database for storing metadata for spring batch tables and postgre for Spring JPA(Used by ItemWriter).</p>
  <p>I have defined two separate properties in properties file.</p>
  <pre><code>postgres.datasource.url=jdbc:postgresql://urlxxx
  postgres.datasource.username=xxx
  postgres.datasource.password=yyy
  
  batch.datasource.url=jdbc:h2:mem:testdb
  batch.datasource.driverClassName=org.h2.Driver
  batch.datasource.username=xxx2
  batch.datasource.password=yyy2
  </code></pre>
  <p>Now, In @configuration file in order to link batch datasource I defined,</p>
  <pre><code>@Primary
  @ConfigurationProperties(prefix=&quot;batch.datasource&quot;)
  @Bean(name=&quot;dataSourceBatch&quot;)
  public DataSource firstDataSource() {
      DataSource ds =  DataSourceBuilder.create().build();
      return ds;
  }
  
  @Bean
    BatchConfigurer configurer(@Qualifier(&quot;dataSourceBatch&quot;) DataSource dataSource){
      return new DefaultBatchConfigurer(dataSource);
    }
  </code></pre>
  <p>However, I didn't used another jpa(postgre) db configuration anywhere else,(planning to link it with JPA).</p>
  <p>When I am trying to run the project, i am getting below exception.</p>
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'batchConfig': Unsatisfied dependency expressed through field 'jobBuilderFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSourceBatch': Requested bean is currently in creation: Is there an unresolvable circular reference?
      at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:797) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:227) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$189.0000000010E4E030.getObject(Unknown Source) ~[na:na]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:408) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$189.0000000010E4E030.getObject(Unknown Source) ~[na:na]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
  </code></pre>
  <p>What the issue? I didnt declare any other datasource so  that spring boot start creating one. Also, How can I decalre the postgre datasource such that it can be used by jpa for interation with DB.</p>
  <p>Sorry, if this question sound very basic, I am noob in spring.</p>
  <p>Added batchconfig</p>
  <pre><code>@Configuration
  @EnableScheduling
  public class BatchConfig {
          @Autowired
          private JobBuilderFactory jobBuilderFactory;
  
          @Autowired
          private StepBuilderFactory stepBuilderFactory;
  
       
          @Bean
          ItemReader&lt;ABC&gt; reader() {
              return new LocationReader();
          }
          
          @Bean
          public RestTemplate restTemplate() {
              return new RestTemplate();
          }
  
          @Bean
          ItemProcessor&lt;ABC,ABC&gt; moviesItemProcessor() {
              return new LocationProcessor();
          }
  
          @Bean
          ItemWriter&lt;ABC&gt; Writer(){
  
              return new LocationWriter();
              
          }
  
  
          @Bean
          public Step step1(ItemReader&lt;ABC&gt; reader,
                              ItemProcessor&lt;ABC,ABC&gt; processor,
                              ItemWriter&lt;ABC&gt; writer) throws Exception {
              return stepBuilderFactory.get(&quot;step1&quot;)
                      .&lt;ABC, ABC&gt;chunk(10)
                      .reader(reader)
                      .processor(processor)
                      .writer(writer).allowStartIfComplete(true)
                      .build();
          }
  
          @Bean
          public Job job(Step step1) throws Exception {
              return jobBuilderFactory.get(&quot;job&quot;)
                      .start(step1(reader(), moviesItemProcessor(), Writer()))
                      .build();
          }
          
          
          
          
          @Primary
          @ConfigurationProperties(prefix=&quot;batch.datasource&quot;)
          @Bean(name=&quot;dataSourceBatch&quot;)
          public DataSource firstDataSource() {
              DataSource ds =  DataSourceBuilder.create().build();
              return ds;
          }
         
            @Bean
            BatchConfigurer configurer(@Qualifier(&quot;dataSourceBatch&quot;) DataSource dataSource){
              return new DefaultBatchConfigurer(dataSource);
            }
  
          
  //
  //      @Bean
  //      public JobRepository jobRepository() throws Exception {
  //          final JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
  //          factory.setDatabaseType(DatabaseType.H2.getProductName());
  //          factory.setDataSource(firstDataSource());
  //          return factory.getObject();
  //      }
  //
  //      @Bean
  //      public SimpleJobLauncher jobLauncher() throws Exception {
  //          final SimpleJobLauncher launcher = new SimpleJobLauncher();
  //          launcher.setJobRepository(jobRepository());
  //          return launcher;
  //      }
          
  
          @Autowired
          JobLauncher jol;
  
          
          @Autowired
          Job job;
           
          
          @Scheduled(cron = &quot;0 */1 * * * ?&quot;)
          public void perform() throws Exception 
          {
              JobParameters params = new JobParametersBuilder()
                      .addString(&quot;JobID&quot;, String.valueOf(System.currentTimeMillis()))
                      .toJobParameters();
              jol.run(job, params);
          }
  
  }
  </code></pre>