title: Spring Batch: datasource for Batch and datasource for step
body: <p>I need to get access to two datasources:</p>
  <ul>
  <li>Spring batch repository: in memory H2</li>
  <li>My step needs to get access to a <code>mssql</code>.</li>
  </ul>
  <p>I've seen several example over there about how to create a custom <code>batch configurer</code>.</p>
  <p>However, sometimes:</p>
  <ol>
  <li><code>implements BatchConfigurer</code></li>
  <li><code>extends DefaultBatchConfigurer</code></li>
  </ol>
  <p>Currently, my configuration files are:</p>
  <pre><code>.
  ├── steps
  │   └── MssqlBatchConfigurationStep.java
  └── MainJobBatchConfiguration.java
  </code></pre>
  <p>My step configuration is:</p>
  <pre><code>@Configuration
  public class MssqlBatchConfigurationStep {
  
      private DataSource dataSource;
  
      /**
       *
       * @param dataSource
       */
      public MssqlBatchConfigurationStep(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  
      /**
       *
       * @return
       */
      public ItemReader&lt;Unitat&gt; reader() {
          String sql = &quot;SELECT operation,update_time,table_name,rowid,user_login,user_name, user_ip,application_name,application_version,new_value,old_value FROM renovastorage.data_log&quot;;
  
          JdbcCursorItemReader&lt;Unitat&gt; jdbcCursorItemReader = new JdbcCursorItemReader&lt;&gt;();
          jdbcCursorItemReader.setDataSource(this.dataSource);
          jdbcCursorItemReader.setSql(sql);
          jdbcCursorItemReader.setVerifyCursorPosition(false);
          jdbcCursorItemReader.setRowMapper(new UnitatRowMapper());
  
          return jdbcCursorItemReader;
      }
  
      /**
       *
       * @return
       */
      public ItemWriter&lt;UnitatDenormalized&gt; writer() {
          // write to solr
          return null;
      }
  
  }
  </code></pre>
  <p>The problem here is that, this step is getting default datasource. This datasource is the same that is got by <code>Spring Batch</code>.</p>
  <p>In order to solve that, I want to create a <code>&quot;Batch Configurer&quot;</code> in order to get specific datasource instead of the default one.</p>
  <p>Here you can see my job configuration:</p>
  <pre><code>@Configuration
  @EnableBatchProcessing
  // @EnableScheduling
  public class MainJobBatchConfiguration {
  
      private JobBuilderFactory jobBuilderFactory;
      private StepBuilderFactory stepBuilderFactory;
  
      private MssqlBatchConfigurationStep unitatBatchStep;
  
      /**
       *
       * @param jobBuilderFactory
       * @param stepBuilderFactory
       */
      public MainJobBatchConfiguration(
          JobBuilderFactory jobBuilderFactory,
          StepBuilderFactory stepBuilderFactory,
          MssqlBatchConfigurationStep unitatBatchStep
      ) {
          this.jobBuilderFactory = jobBuilderFactory;
          this.stepBuilderFactory = stepBuilderFactory;
          this.unitatBatchStep = unitatBatchStep;
      }
  
  
      /**
       *
       * @return
       */
      @Bean
      public Step step() {
          return this.stepBuilderFactory
              .get(&quot;mssql&quot;)
              .&lt;Unitat, UnitatDenormalized&gt;chunk(10)
              .reader(this.unitatBatchStep.reader())
              .writer(this.unitatBatchStep.writer())
              .build();
      }
  
      /**
       *
       * @param step
       * @return
       */
      @Bean
      public Job job(Step step) {
          Job job = this.jobBuilderFactory.get(&quot;job1&quot;)
              .flow(step)
              .end()
              .build();
          return job;
      }
  
  }
  </code></pre>