spring boot 多数据源 + 事务控制
1,首先在启动类加上@EnableTransactionManagement注解
package cn.bforce.common; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.ComponentScan; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @ComponentScan(basePackages={"cn.bforce.common"}) @EnableCaching @EnableTransactionManagement public class YuntuSysBaseApplication { public static void main(String[] args) { SpringApplication.run(YuntuSysBaseApplication.class, args); } }
2,application.properties文件配置的双数据源文件配置
#datasource b-force spring.datasource.bf.driver-class-name=com.mysql.jdbc.Driver spring.datasource.bf.url=jdbc:mysql://192.168.18.221:3306/b-force?characterEncoding=utf8&useSSL=true spring.datasource.bf.username=root spring.datasource.bf.password=root spring.datasource.bfscrm.driver-class-name=com.mysql.jdbc.Driver spring.datasource.bfscrm.url=jdbc:mysql://192.168.18.221:3306/b-force-scrm?characterEncoding=utf8&useSSL=true spring.datasource.bfscrm.username=root spring.datasource.bfscrm.password=root
3,JavaConfig 首先建立Java配置类,为其添加上注解@Configuration
。并实现如下方法。
package cn.bforce.common.persistence.datasource; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @Configuration public class GlobalDataConfiguration { @Bean(name = "bfDataSource") @Qualifier("bfDataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource.bf") public DataSource primaryDataSource() { System.out.println("-------------------- bfDataSource init ---------------------"); return DataSourceBuilder.create().build(); } @Bean(name = "bfscrmDataSource") @Qualifier("bfscrmDataSource") @ConfigurationProperties(prefix = "spring.datasource.bfscrm") public DataSource secondaryDataSource() { System.out.println("-------------------- bfscrmDataSource init ---------------------"); return DataSourceBuilder.create().build(); } @Bean(name = "bfJdbcTemplate") public JdbcTemplate bfJdbcTemplate(@Qualifier("bfDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "bfscrmJdbcTemplate") public JdbcTemplate bfscrmscrmJdbcTemplate(@Qualifier("bfscrmDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } /******配置事务管理********/ @Bean public PlatformTransactionManager bfTransactionManager(@Qualifier("bfDataSource")DataSource prodDataSource) { return new DataSourceTransactionManager(prodDataSource); } @Bean public PlatformTransactionManager bfscrmTransactionManager(@Qualifier("bfscrmDataSource")DataSource sitDataSource) { return new DataSourceTransactionManager(sitDataSource); } }
4,使用。
@Transactional(value = "bfscrmTransactionManager",readOnly=true) public DataObject doLoad(Serializable rowId) { return shopLbsRepository.doLoad(rowId); } @Transactional(value = "bfscrmTransactionManager") public int doUpdate(String v) { shopLbsRepository.doUpdate(v); int a = 8/0; return 1; }
总结:测试可用的。
****最后说明:如果要用指定的那个数据源,注解 JdbcTemplate 的时候。看如下代码。
@Autowired @Qualifier("bfscrmJdbcTemplate") protected JdbcTemplate jdbcTemp; @Autowired @Qualifier("bfscrmDataSource") protected DataSource dataSource;
@Autowired @Qualifier("bfJdbcTemplate") protected JdbcTemplate jdbcTemp; @Autowired @Qualifier("bfDataSource") protected DataSource dataSource;
************************
博主给自己的小程序打个广告,支付宝搜索: 变换购物助手
淘宝,天猫购物最高返利谢谢大家使用支持