multiple datasource config
Hi Harshit S.
project structure:
multiple datasource config as follows:
step 1:
step 2:add a datasource configuration class
@Configuration
public class DataSourceConfig {
// ape datasource config
@Value("${spring.ape-datasource.driver-class-name}")
private String apeDriverClassName; @Value("${spring.ape-datasource.url}")
private String apeDBUrl; @Value("${spring.ape-datasource.username}")
private String apeDBUsername; @Value("${spring.ape-datasource.password}")
private String apeDBPassword; // DBQ datasource config
@Value("${spring.dbq-datasource.url}")
private String dbqDBUrl; @Value("${spring.dbq-datasource.username}")
private String dbqDBUsername; @Value("${spring.dbq-datasource.password}")
private String dbqDBPassword; @Value("${spring.dbq-datasource.driver-class-name}")
private String dbqDriverClassName; @Primary
@Bean(name = "apeDataSource")
@Qualifier("apeDataSource")
public DataSource apeDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setPoolName("ape pool");
dataSource.setDriverClassName(apeDriverClassName);
dataSource.setJdbcUrl(apeDBUrl);
dataSource.setUsername(apeDBUsername);
dataSource.setPassword(apeDBPassword);
dataSource.setAutoCommit(true);
dataSource.setMaximumPoolSize(20);
return dataSource;
} @Bean(name = "dbqDataSource")
@Qualifier("dbqDataSource")
public DataSource dbqDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setPoolName("DBQ pool");
dataSource.setDriverClassName(dbqDriverClassName);
dataSource.setJdbcUrl(dbqDBUrl);
dataSource.setUsername(dbqDBUsername);
dataSource.setPassword(dbqDBPassword);
dataSource.setAutoCommit(true);
dataSource.setMaximumPoolSize(20);
return dataSource;
} }
Step3: add Jpa config class
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "apeManagerFactory",
transactionManagerRef = "apeTransactionManager",
basePackages = {"com.cn.ano2ape.repository.ape"}
)
public class ApeJpaConfig {
@Autowired
@Qualifier("apeDataSource")
private DataSource apeDataSource; @Autowired
private JpaProperties jpaProperties; @Autowired
private HibernateProperties hibernateProperties; @Primary
@Bean(name = "apeEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return apexaEntityManagerFactory(builder).getObject().createEntityManager();
} @Primary
@Bean(name = "apeManagerFactory")
public LocalContainerEntityManagerFactoryBean apexaEntityManagerFactory(EntityManagerFactoryBuilder builder) {
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
return builder.dataSource(apeDataSource).properties(properties).packages("com.cn.ano2ape.model.ape").build();
} @Primary
@Bean(name = "apeTransactionManager")
PlatformTransactionManager apexaTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(apexaEntityManagerFactory(builder).getObject());
}
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "dbqManagerFactory",
transactionManagerRef = "dbqTransactionManager",
basePackages = {"com.cn.ano2ape.repository.dbq"}
)
public class DbqJpaConfig {
@Autowired
@Qualifier("dbqDataSource")
private DataSource dbqDataSource; @Autowired
private JpaProperties jpaProperties; @Autowired
private HibernateProperties hibernateProperties; @Bean(name = "dbqEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return dbqEntityManagerFactory(builder).getObject().createEntityManager();
} @Bean(name = "dbqManagerFactory")
public LocalContainerEntityManagerFactoryBean dbqEntityManagerFactory(EntityManagerFactoryBuilder builder) {
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
return builder.dataSource(dbqDataSource).properties(properties).packages("com.cn.ano2ape.model.dbq").build();
} @Bean(name = "dbqTransactionManager")
PlatformTransactionManager dbqTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(dbqEntityManagerFactory(builder).getObject());
}
}
end.
multiple datasource config的更多相关文章
- jpa datasource config
application.properties spring.datasource.driverClassName= spring.datasource.url= spring.datasource.u ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, Environment
The key component of MyBatis is SqlSessionFactory from which we get SqlSession and execute the mappe ...
- spring cloud 使用spring cloud bus自动刷新配置
Spring Cloud Bus提供了批量刷新配置的机制,它使用轻量级的消息代理(例如RabbitMQ.Kafka等)连接分布式系统的节点,这样就可以通过Spring Cloud Bus广播配置的变化 ...
- spring cloud config--client
概述 之前我们简单的搭建了一个单点的config-server服务,实现配置文件的统一管理,本次文章将实现config-client是如何从config-server中获取到统一配置文件信息的 1.创 ...
- spring cloud Config--server
概述 使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spr ...
- springboot动态多数据源切换
application-test.properties #datasource -- mysql multiple.datasource.master.url=jdbc:mysql://localho ...
- 六、spring boot 1.5.4 配置多数据源
spring boot 已经支持多数据源配置了,无需网上好多那些编写什么类的,特别麻烦,看看如下解决方案,官方的,放心! 1.首先定义数据源配置 #=====================multi ...
- Spring基础知识
Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...
- Spring PropertyPlaceholderConfigurer数据库配置
pom.xml中添加依赖 <!-- mysql-connector-java --> <dependency> <groupId>mysql</groupId ...
随机推荐
- apache traffic server安装
wget http://mirrors.hust.edu.cn/apache/trafficserver/trafficserver-7.1.1.tar.bz2 tar -jxvf trafficse ...
- 设备中LPC2368芯片个例参数问题导致故障的分析
最近公司的设备客户报告在终端客户那里出现了板卡加热不受控,出现了持续加热导致设备一些贵重部件损坏.由于历史上很多现场问题,板卡什么拆到别的地方搭复现平台,基本都是以失败告终,所以出差去现场分析. 过程 ...
- 6U VPX 加固智能计算异构服务器
6U VPX 加固智能计算异构服务器 北京太速科技有限公司在线客服:QQ:448468544 公司网站:www.orihard.com联系电话:15084122580
- RHEL6查看运行级别和grub加密阻止进入单用户模式
1.查看当前运行级别 [root@cluster01 ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.5 ( ...
- ui自动化之selenium操作(五)简单元素操作--续
1. 多窗口切换 有时候需要在多窗口切换,webdriver提供了switch_to_window()方法支持切换窗口: from selenium import webdriver import o ...
- c++ primer 5th(中文版)勘误
\(P_{158}\) "末位大于 3" 改为 "末位大于等于 3" \(P_{302}\) \(P_{319}\) // 添加元素用光多余容量 while ( ...
- thinkphp一般数据库操作
引入命名空间 插入 更新 查询 删除 一些支持命令行的操作 清空操作 分库操作 分库相关配置---在config.php中进行 使用: 参数绑定 占位符绑定 第一句后半拉
- Codeforces Round #568 (Div. 2) C2. Exam in BerSU (hard version)
链接: https://codeforces.com/contest/1185/problem/C2 题意: The only difference between easy and hard ver ...
- SQL 行转列(列的值不规则的数目)
--创建一个临时表用来存储数据 create table #tmp_SNValue_Table (FieldName nvarchar(20), [Value] nvarchar(max)) inse ...
- 关于同时可用git命令clone和TortoiseGit拉取代码不需要密码
工作需要在windows7下使用git分布式版本控制系统,需要同时可以在git命令行模式或TortoiseGit拉取代码而不需要每次输入密码. 这时候需要同时安装git和TortoiseGit. gi ...