springboot always read data from primary datasourc

2019-09-06 09:53发布

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.

http://www.baeldung.com/spring-data-jpa-multiple-databases

The current problem with my app is it is always reading data from the primary datasource(emwbis). I've written below code.

Model classes for primary and backup datasources:

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;

Datasource config classes for primary and backup datasources:

@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 {

Repository interfaces for primary and backup datasources:

@Transactional
public interface CrsBISRepository extends JpaRepository<CrsBIS, String> {
    public CrsBIS findById(String id);

}

@Transactional
public interface CrBisBackupRepository extends JpaRepository<CrsBIS, String>{
    public CrsBIS findById(String id);
}

Persistent db proeprties file :

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

Controller class to test both the datasources :

@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;
    }

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 getByIdFromBackup method in controller class to read the data from secondary database (backupemwbis).

Can someone please let me know the mistake in my code? Or you can suggest/guide me to achieve my goal?

2条回答
神经病院院长
2楼-- · 2019-09-06 09:56

From the first configuration file you're creating a primary datasource bean definition with the name myDatasource and in the second emf you're injecting the same datasource reference. The Bean causing the problem is this

@Bean
@Primary
public DataSource myDataSource()

Just change the second Bean datasource name and use it in the second EMF.

public class BackupCrsBISDatabaseConfig {

    ...
    @Bean
    public DataSource backupDS() {
    ....

    @Bean
    public LocalContainerEntityManagerFactoryBean crsBISBackUpEntityManager() {
      ....
      em.setDataSource(backupDS());
    }
}

Hope this fixes it.

查看更多
走好不送
3楼-- · 2019-09-06 10:03

You have to explicitly request a TransactionManager implementation in your @Transactional usage:

@Transactional("crsBISTransactionManager")
//..

@Transactional("crsBISBackupTransactionManager")
//..
查看更多
登录 后发表回答