title: springboot always read data from primary datasource
body: <p>My springboot app tries to read data from two datasources(emwbis and backupemwbis). I've followed the below link in configuring my springboot app to read data from two different datasources.</p>
  
  <p><a href="http://www.baeldung.com/spring-data-jpa-multiple-databases" rel="nofollow noreferrer">http://www.baeldung.com/spring-data-jpa-multiple-databases</a></p>
  
  <p>The current problem with my app is it is always reading data from the primary datasource(emwbis). I've written below code.</p>
  
  <p>Model classes for primary and backup datasources:</p>
  
  <pre><code>package com.jl.models.primary;
  @Entity
  @Table(name = "crsbis",schema="emwbis")
  @Data
  public class CrsBIS {
  
      @Id
      private String id;
  
      @NotNull
      private String email;
  
  package com.jl.models.backup;
  
  import lombok.Data;
  
  @Entity
  @Table(name = "crsbis",schema="backupemwbis")
  @Data
  public class CrsBIS {
  
      @Id
      private String id;
  
      @NotNull
      private String email;
  </code></pre>
  
  <p>Datasource config classes for primary and backup datasources:</p>
  
  <pre><code>@Configuration
  @PropertySource("classpath:persistence-multiple-db.properties")
  @EnableJpaRepositories(basePackages = "com.jl.dao.backup", entityManagerFactoryRef = "crsBISBackUpEntityManager", transactionManagerRef = "crsBISBackupTransactionManager")
  public class BackupCrsBISDatabaseConfig {
  
  @Configuration
  @PropertySource("classpath:persistence-multiple-db.properties")
  @EnableJpaRepositories(basePackages = "com.jl.dao.primary", entityManagerFactoryRef = "crsBISEntityManager", transactionManagerRef = "crsBISTransactionManager")
  public class CrsBISDatabaseConfig {
  </code></pre>
  
  <p>Repository interfaces for primary and backup datasources:</p>
  
  <pre><code>@Transactional
  public interface CrsBISRepository extends JpaRepository&lt;CrsBIS, String&gt; {
      public CrsBIS findById(String id);
  
  }
  
  @Transactional
  public interface CrBisBackupRepository extends JpaRepository&lt;CrsBIS, String&gt;{
      public CrsBIS findById(String id);
  }
  </code></pre>
  
  <p>Persistent db proeprties file :</p>
  
  <pre><code>jdbc.driverClassName=com.mysql.jdbc.Driver
  crsbis.jdbc.url=jdbc:mysql://localhost:3306/emwbis
  backupcrsbis.jdbc.url=jdbc:mysql://localhost:3306/backupemwbis
  jdbc.user=root
  jdbc.pass=Password1
  </code></pre>
  
  <p>Controller class to test both the datasources :</p>
  
  <pre><code>@Controller
  public class CrsBISController {
  
      @Autowired
      private CrsBISRepository crsBISRepository;
  
      @Autowired
      private CrBisBackupRepository crsBackupRepository;
  
  @RequestMapping("/get-by-id")
      @ResponseBody
      public String getById(String id){
          String email="";
          try{
              CrsBIS crsBIS = crsBISRepository.findById(id);
              email = String.valueOf(crsBIS.getEmail());
          }catch (Exception e) {
              e.printStackTrace();
              return "id not found!";
          }
          return "The email is : "+email;
      }
  
      @RequestMapping("/get-by-id-backup")
      @ResponseBody
      public String getByIdFromBackup(String id){
          String email="";
          try{
              com.jl.models.backup.CrsBIS crsBIS = crsBackupRepository.findById(id);
              email = String.valueOf(crsBIS.getEmail());
          }catch (Exception e) {
              // TODO: handle exception
              e.printStackTrace();
              return "id not found!";
          }
          return "The email is : "+email;
      }
  </code></pre>
  
  <p>Although, I've separated the database schemas in the model classes and in the database config file, both the methods in the controller class hit the same database (emwbis). I want <code>getByIdFromBackup</code> method in controller class to read the data from secondary database (backupemwbis).</p>
  
  <p>Can someone please let me know the mistake in my code? Or you can suggest/guide me to achieve my goal?</p>