分库分表之ShardingSphere 支持分布式事务

发布于:2021-06-23 06:33:34

1. 问题


分库分表多数据源如何支持分布式事务


?


2. jar引入




com.alibaba
druid
1.1.10




org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.0


org.apache.shardingsphere
sharding-jdbc-spring-boot-starter
4.0.0-RC3



io.shardingsphere
sharding-transaction-spring-boot-starter
3.1.0


?


3.? sharding配置


见之前的文章


?


4. 添加@EnableTransactionManagement注解



@EnableTransactionManagement
@SpringBootApplication(scanBasePackages = "com.example.shard")
public class ShardJdbcApplication {


?


5.? Service方法添加注解



@ShardingTransactionType(TransactionType.XA)
@Transactional(rollbackFor = Exception.class)
@Override
public int insert(AppAuthRegister record1, AppAuthRegister record2) {
this.getSqlSessionTemplate()
.insert("com.example.shard.mapper.AppAuthRegisterMapper.insert", record1);
this.getSqlSessionTemplate()
.insert("com.example.shard.mapper.AppAuthRegisterMapper.insert", record2);
System.out.println(100 / 0);//测试回滚,统一提交的话,将这行注释掉就行了
return 1;
}

??



测试发现,回滚操作成功执行?

相关推荐

最新更新

猜你喜欢