springboot 总是从主数据源读取数据 [英] springboot always read data from primary datasource
问题描述
我的 springboot 应用程序尝试从两个数据源(emwbis 和 backupemwbis)读取数据.我已经按照以下链接配置我的 springboot 应用程序以从两个不同的数据源读取数据.
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-多个数据库
我的应用程序当前的问题是它总是从主数据源 (emwbis) 读取数据.我写了下面的代码.
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);
}
持久数据库属性文件:
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;
}
尽管我已经将模型类和数据库配置文件中的数据库模式分开,但控制器类中的两种方法都访问同一个数据库 (emwbis).我希望控制器类中的 getByIdFromBackup
方法从辅助数据库(backupemwbis)读取数据.
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?
推荐答案
从第一个配置文件创建一个名为 myDatasource 的主数据源 bean 定义,并在第二个 emf 中注入相同的数据源引用.导致问题的Bean是这个
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()
只需更改第二个 Bean 数据源名称并在第二个 EMF 中使用它即可.
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());
}
}
希望这能解决问题.
这篇关于springboot 总是从主数据源读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!