datasource数据源_SpringBoot 2.0 集成Atomikos、Durid 实现多数据源、分布式事务

作者:殷天文

链接:https://www.jianshu.com/p/f9bac5822d30

之前的 《spring 动态切换、添加数据源实现以及源码浅析》 (https://www.jianshu.com/p/0a485c965b8b)中介绍了如何使用 spring 提供的 AbstractRoutingDataSource 配置多数据源,有了多数据源自然要管理事务的一致性。

上篇文章中提到过配置多数据源的两种方式

  1. 使用AbstractRoutingDataSource

  2. 配置多个 SqlSessionFactory

前言

阅读了一下spring的源码,由于 spring 事务的机制,在开启事务之前spring 会去创建当前数据源的 事务object,直到事务提交,spring 都不会在乎你是否切换了数据源。这就导致了,使用 AbstractRouting DataSource 方式开启事务时,切换数据源不生效。

关于如何解决这个问题,感兴趣的朋友可以去阅读一下:https://www.jianshu.com/p/61e8961c6154

本文只讨论上述第二种方式结合 atomikos 管理多数据源事务。

Atomikos

来自:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-jta.html

Atomikos is a popular open source transaction manager which can be embedded into your Spring Boot application. You can use the spring-boot-starter-jta-atomikos Starter to pull in the appropriate Atomikos libraries. Spring Boot auto-configures Atomikos and ensures that appropriate depends-on settings are applied to your Spring beans for correct startup and shutdown ordering.

By default, Atomikos transaction logs are written to a transaction-logs directory in your application's home directory (the directory in which your application jar file resides). You can customize the location of this directory by setting a spring.jta.log-dir property in your application.properties file. Properties starting with spring.jta.atomikos.properties can also be used to customize the Atomikos UserTransactionServiceImp. See the AtomikosProperties Javadoc for complete details.

引入spring-boot-starter-jta-atomikos,spring boot 为我们自动配置

Atomikos,我们可以通过 spring.jta.xxx 修改默认配置。

Talk is cheap. Show me the code

  • demo源码:https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/spring-atomikos

  • 添加 maven 依赖

org.springframework.boot

spring-boot-starter-jta-atomikos

com.alibaba

druid-spring-boot-starter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值