title: How to java-configure separate datasources for spring batch data and business data? Should I even do it?
body: <p>My main job does only read operations and the other one does some writing but on <code>MyISAM engine</code> which ignores transactions, so I wouldn't require necessarily transaction support. How can I configure <code>Spring Batch</code> to have its own datasource for the <code>JobRepository</code>, separate from the one holding the business data? The initial one datasource-configurations is done like the following: </p>
  
  <pre><code>@Configuration
  public class StandaloneInfrastructureConfiguration {
  
      @Autowired
      Environment env;
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "org.podcastpedia.batch.*" });
  
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalJpaProperties());
  
        return em;
      }
  
      Properties additionalJpaProperties() {
            Properties properties = new Properties();
            properties.setProperty("hibernate.hbm2ddl.auto", "none");
            properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
            properties.setProperty("hibernate.show_sql", "true");
  
            return properties;
      }
  
      @Bean
      public DataSource dataSource(){
  
         return DataSourceBuilder.create()
                  .url(env.getProperty("db.url"))
                  .driverClassName(env.getProperty("db.driver"))
                  .username(env.getProperty("db.username"))
                  .password(env.getProperty("db.password"))
                  .build();          
      }
  
      @Bean
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
  
        return transactionManager;
      }
  }
  </code></pre>
  
  <p>and then it is imported in the <code>Job</code>'s configuration class where the <code>@EnableBatchProcessing</code> annotation automagically makes use of it. My initial thought was to try to set the configuration class extend the <code>DefaultBatchConfigurer</code>, but then I get a </p>
  
  <blockquote>
    <p>BeanCurrentlyInCreationException ( org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name jobBuilders: Requested bean is currently in creation: Is there an unresolvable circular reference?):</p>
  </blockquote>
  
  <pre><code>@Configuration
  @EnableBatchProcessing
  @Import({StandaloneInfrastructureConfiguration.class, NotifySubscribersServicesConfiguration.class})
  public class NotifySubscribersJobConfiguration extends DefaultBatchConfigurer {
  
      @Autowired
      private JobBuilderFactory jobBuilders;
  
      @Autowired
      private StepBuilderFactory stepBuilders;
  
      @Autowired
      private DataSource dataSource;
  
      @Autowired
      Environment env;
  
      @Override
      @Autowired
      public void setDataSource(javax.sql.DataSource dataSource) {
          super.setDataSource(batchDataSource());
      }
  
      private DataSource batchDataSource(){          
         return DataSourceBuilder.create()
                  .url(env.getProperty("batchdb.url"))
                  .driverClassName(env.getProperty("batchdb.driver"))
                  .username(env.getProperty("batchdb.username"))
                  .password(env.getProperty("batchdb.password"))
                  .build();          
      } 
  
      @Bean
      public ItemReader&lt;User&gt; notifySubscribersReader(){
  
          JdbcCursorItemReader&lt;User&gt; reader = new JdbcCursorItemReader&lt;User&gt;();
          String sql = "select * from users where is_email_subscriber is not null";
  
          reader.setSql(sql);
          reader.setDataSource(dataSource);
          reader.setRowMapper(rowMapper());       
  
          return reader;
      }
  ........
  }   
  </code></pre>
  
  <p>Any thoughts are more than welcomed. The project is available on GitHub - <a href="https://github.com/podcastpedia/podcastpedia-batch" rel="noreferrer">https://github.com/podcastpedia/podcastpedia-batch</a></p>
  
  <p>Thanks a bunch.</p>