title: Data Migration from One Db to another using Spring batch and Spring boot
body: <p>Im new to spring framework and I Want to migrate data from One DB to another using Spring boot n Batch</p>
  <p>Im trying to read from an Mysql db with an item reader and write it to an Oracle db using an item writer of the same job.</p>
  <p>Im able to read the data from the Mysql db but unable to write it to Oracle Db as the writer is trying it write it in Mysql Db itself.</p>
  <p>Im not sure why the connection is not switching to Oracle db.</p>
  <p>Please help me over here what im doing wrong.</p>
  <p>Application properties</p>
  <pre><code>server.port=8082
  
  spring.batch.job.enabled= false
  
  spring.datasource.url = jdbc:mysql://localhost:3306/projectdb1
  spring.datasource.username = root
  spring.datasource.password = Mech_2015
  spring.datasource.driverClassName = com.mysql.jdbc.Driver
  
  
  #second db2 ...
  db2.datasource.url = jdbc:oracle:thin:@localhost:1521:xe 
  db2.datasource.username = system
  db2.datasource.password = Mech_2015
  db2.datasource.driverClassName = oracle.jdbc.driver.OracleDriver
  
  </code></pre>
  <p>Entity Managers</p>
  <pre><code>package com.techprimers.springbatchexample1.entity.manger;
  @Configuration
  @EnableJpaRepositories(entityManagerFactoryRef = &quot;radiusEntityManager&quot;, transactionManagerRef = &quot;radiusTransactionManager&quot;, basePackages = &quot;com.techprimers.springbatchexample1.repository.userrepo&quot;)
  public class RadiusConfig {
  
      private final PersistenceUnitManager persistenceUnitManager;
  
      public RadiusConfig(ObjectProvider&lt;PersistenceUnitManager&gt; persistenceUnitManager) {
          this.persistenceUnitManager = persistenceUnitManager.getIfAvailable();
      }
  
      @Bean
      @ConfigurationProperties(&quot;spring.jpa&quot;)
      public JpaProperties radiusJpaProperties() {
          return new JpaProperties();
      }
  
      @Bean
      @Primary
      @ConfigurationProperties(&quot;spring.datasource&quot;)
      public DataSourceProperties radiusDataSourceProperties() {
          return new DataSourceProperties();
      }
  
      @Bean
      @ConfigurationProperties(prefix = &quot;spring.datasource.properties&quot;)
      public DataSource radiusDataSource() {
          return (DataSource) radiusDataSourceProperties().initializeDataSourceBuilder().type(DataSource.class).build();
      }
  
      @Bean(name = &quot;radiusEntityManager&quot;)
      public LocalContainerEntityManagerFactoryBean radiusEntityManager(JpaProperties radiusJpaProperties) {
          EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(radiusJpaProperties);
          return builder.dataSource(radiusDataSource()).packages(User.class).persistenceUnit(&quot;userDs&quot;).build();
      }
  
      @Bean
      public JpaTransactionManager radiusTransactionManager(EntityManagerFactory radiusEntityManager) {
          return new JpaTransactionManager(radiusEntityManager);
      }
  
      private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties radiusJpaProperties) {
          JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(radiusJpaProperties);
          return new EntityManagerFactoryBuilder(jpaVendorAdapter, radiusJpaProperties.getProperties(),
                  this.persistenceUnitManager);
      }
  
      private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
          AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
          adapter.setShowSql(jpaProperties.isShowSql());
          adapter.setDatabase(jpaProperties.getDatabase());
          adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
          adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
          return adapter;
      }
  
  }
  
  </code></pre>
  <pre><code>package com.techprimers.springbatchexample1.entity.manger;
  
  @Configuration
  @EnableJpaRepositories(entityManagerFactoryRef = &quot;esbEntityManager&quot;, transactionManagerRef = &quot;esbDetailsTransactionManager&quot;, basePackages = &quot;com.techprimers.springbatchexample1.repository.userdetails&quot;)
  public class EsbConfig {
  
      private final PersistenceUnitManager persistenceUnitManager;
  
      public EsbConfig(ObjectProvider&lt;PersistenceUnitManager&gt; persistenceUnitManager) {
          this.persistenceUnitManager = persistenceUnitManager.getIfAvailable();
      }
  
      @Bean
      @ConfigurationProperties(&quot;db2.jpa&quot;)
      public JpaProperties esbJpaProperties() {
          return new JpaProperties();
      }
  
      @Bean
      @ConfigurationProperties(&quot;db2.datasource&quot;)
      public DataSourceProperties esbDataSourceProperties() {
          return new DataSourceProperties();
      }
  
      @Bean
      @ConfigurationProperties(prefix = &quot;db2.datasource.properties&quot;)
      public DataSource esbDataSource() {
          return (DataSource) esbDataSourceProperties().initializeDataSourceBuilder().type(DataSource.class).build();
      }
  
      @Bean(name = &quot;esbEntityManager&quot;)
      public LocalContainerEntityManagerFactoryBean esbEntityManager(JpaProperties esbJpaProperties) {
          EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(esbJpaProperties);
          return builder.dataSource(esbDataSource()).packages(UserDetails.class).persistenceUnit(&quot;userDetailDs&quot;).build();
      }
  
      @Bean
      public JpaTransactionManager esbTransactionManager(EntityManagerFactory esbEntityManager) {
          return new JpaTransactionManager(esbEntityManager);
      }
  
      private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties esbJpaProperties) {
          JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(esbJpaProperties);
          return new EntityManagerFactoryBuilder(jpaVendorAdapter, esbJpaProperties.getProperties(),
                  this.persistenceUnitManager);
      }
  
      private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
          AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
          adapter.setShowSql(jpaProperties.isShowSql());
          adapter.setDatabase(jpaProperties.getDatabase());
          adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
          adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
          return adapter;
      }
  
  }
  
  </code></pre>
  <p>Batch Config</p>
  <pre><code>package com.techprimers.springbatchexample1.config;
  
  @Configuration
  @EnableBatchProcessing
  public class SpringBatchConfig {
  
      private static final Logger LOGGER = LoggerFactory.getLogger(SpringBatchConfig.class);
  
  
      @Bean
      @Primary
      @Qualifier(&quot;radiusDatasource&quot;)
      @ConfigurationProperties(prefix = &quot;spring.datasource&quot;)
      DataSource mysqlDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      @ConfigurationProperties(prefix = &quot;db2.datasource&quot;)
      @Qualifier(&quot;esbDatasource&quot;)
      DataSource oracleDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      public InspectionProcessor processor() {
          return new InspectionProcessor();
      }
  
      @Bean
      public JdbcCursorItemReader&lt;User&gt; reader() {
          JdbcCursorItemReader&lt;User&gt; cursorItemReader = new JdbcCursorItemReader&lt;&gt;();
          cursorItemReader.setDataSource(mysqlDataSource());
          cursorItemReader.setSql(&quot;SELECT ID,CNAME,SID,CREATEDDATE,COMPLETEDDATE FROM INSPECTION&quot;);
          cursorItemReader.setRowMapper(new InspectionDetailRowmapper());
          return cursorItemReader;
      }
  
      private static final String QUERY_INSERT_STUDENT = &quot;INSERT &quot;
              + &quot;INTO inspect(id,cname,completeddate,createddate,lastupdateddate,sid) &quot; + &quot;VALUES (?,?,?,?,?,?)&quot;;
  
      @Bean
      ItemWriter&lt;UserDetails&gt; databaseItemWriter(DataSource dataSource, NamedParameterJdbcTemplate jdbcTemplate) {
  
          LOGGER.info(&quot;Starting writer&quot;);
  
          JdbcBatchItemWriter&lt;UserDetails&gt; databaseItemWriter = new JdbcBatchItemWriter&lt;&gt;();
          databaseItemWriter.setDataSource(oracleDataSource());
          databaseItemWriter.setJdbcTemplate(jdbcTemplate);
  
          LOGGER.info(&quot; writer&quot;);
  
          databaseItemWriter.setSql(QUERY_INSERT_STUDENT)
  
          ItemPreparedStatementSetter&lt;UserDetails&gt; valueSetter = new UserDetailsPreparedStatementSetter();
          databaseItemWriter.setItemPreparedStatementSetter(valueSetter);
  
  
          return databaseItemWriter;
      }
  
      @Bean
      Step dataMigrationStep(ItemReader&lt;User&gt; reader, ItemProcessor&lt;User, UserDetails&gt; processor,
              ItemWriter&lt;UserDetails&gt; databsaeItemWriter, StepBuilderFactory stepBuilderFactory) {
          return stepBuilderFactory.get(&quot;dataMigrationStep&quot;).&lt;User, UserDetails&gt;chunk(5).reader(reader)
                  .processor(processor).writer(databsaeItemWriter).build();
      }
  
      @Bean
      Job dataMigrationJob(JobBuilderFactory jobBuilderFactory, @Qualifier(&quot;dataMigrationStep&quot;) Step dataMigrationStep) {
          return jobBuilderFactory.get(&quot;csvFileToDatabaseJob&quot;).incrementer(new RunIdIncrementer()).flow(dataMigrationStep)
                  .end().build();
      }
  
  }
  
  </code></pre>