如果采用AT模式,那么每个业务数据库还需建一个undo_log表
-- for AT mode you must to init this sql for you business database. the seata server not need it.CREATE TABLE IF NOT EXISTS `undo_log`(`branch_id`BIGINTNOT NULL COMMENT 'branch transaction id',`xid`VARCHAR(128) NOT NULL COMMENT 'global transaction id',`context`VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',`rollback_info` LONGBLOBNOT NULL COMMENT 'rollback info',`log_status`INT(11)NOT NULL COMMENT '0:normal status,1:defense status',`log_created`DATETIME(6)NOT NULL COMMENT 'create datetime',`log_modified`DATETIME(6)NOT NULL COMMENT 'modify datetime',UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COMMENT ='AT transaction mode undo table';
文章插图
启动
- 源码启动: 执行ServerApplication.java的main方法
- 命令启动: seata-server.sh -h 127.0.0.1 -p 8091 -m db
- -h: 注册到注册中心的ip
- -p: Server rpc 监听端口
- -m: 全局事务会话信息存储模式,file、db、redis,优先读取启动参数
- -n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
- -e: 多环境配置参考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html?
步骤一:添加seata依赖(建议单?。?
- 依赖seata-all
- 依赖seata-spring-boot-starter , 支持yml、properties配置(.conf可删除),内部已依赖seata-all
- 依赖spring-cloud-alibaba-seata , 内部集成了seata,并实现了xid传递
https://seata.io/zh-cn/docs/user/configurations.html
步骤三:数据源代理(不支持自动和手动配置并存)
1、如果使用seata-all
自动配置由注解@EnableAutoDataSourceProxy开启,并可选择jdk proxy或者cglib proxy 。
如果采用XA模式 , @EnableAutoDataSourceProxy(dataSourceProxyMode = "XA")
手动配置参考如下:
@Primary@Bean("dataSource")public DataSource dataSource(DataSource druidDataSource) {//AT 代理 二选一return new DataSourceProxy(druidDataSource);//XA 代理return new DataSourceProxyXA(druidDataSource)}
2、如果使用seata-starter默认就开启了自动代理数据源,无需额外配置
如果使用自动代理数据源时,如果使用XA模式还需要调整配置文件application.yml
seata:data-source-proxy-mode: XA
如果想要关闭seata-spring-boot-starter的数据源自动代理 , 可调整配置文件application.ymlseata:enable-auto-data-source-proxy: false
步骤四:初始化GlobalTransactionScanner自动,引入seata-spring-boot-starter、spring-cloud-starter-alibaba-seata等jar即可
手动
@Beanpublic GlobalTransactionScanner globalTransactionScanner() {String applicationName = this.applicationContext.getEnvironment().getProperty("spring.application.name");String txServiceGroup = this.seataProperties.getTxServiceGroup();if (StringUtils.isEmpty(txServiceGroup)) {txServiceGroup = applicationName + "-fescar-service-group";this.seataProperties.setTxServiceGroup(txServiceGroup);}return new GlobalTransactionScanner(applicationName, txServiceGroup);}
步骤五:业务使用只需在业务方法上加上@GlobalTransactional 注解即可
3. 示例代码
此处用官网的那个案例,调用关系如图:
文章插图
首先,建库建表,脚本如下:
CREATE DATABASE `account`;USE `account`;DROP TABLE IF EXISTS `t_account`;CREATE TABLE `t_account` (`id` int NOT NULL AUTO_INCREMENT,`user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,`amount` double(14,2) DEFAULT '0.00',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;INSERT INTO `t_account` VALUES (1,'1',79.90);DROP TABLE IF EXISTS `undo_log`;CREATE TABLE `undo_log` (`branch_id` bigint NOT NULL COMMENT 'branch transaction id',`xid` varchar(128) NOT NULL COMMENT 'global transaction id',`context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',`rollback_info` longblob NOT NULL COMMENT 'rollback info',`log_status` int NOT NULL COMMENT '0:normal status,1:defense status',`log_created` datetime(6) NOT NULL COMMENT 'create datetime',`log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='AT transaction mode undo table';CREATE DATABASE `order`;USE `order`;DROP TABLE IF EXISTS `t_order`;CREATE TABLE `t_order` (`id` int NOT NULL AUTO_INCREMENT,`order_no` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,`user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,`commodity_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,`count` int DEFAULT '0',`amount` double(14,2) DEFAULT '0.00',PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;DROP TABLE IF EXISTS `undo_log`;CREATE TABLE `undo_log` (`branch_id` bigint NOT NULL COMMENT 'branch transaction id',`xid` varchar(128) NOT NULL COMMENT 'global transaction id',`context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',`rollback_info` longblob NOT NULL COMMENT 'rollback info',`log_status` int NOT NULL COMMENT '0:normal status,1:defense status',`log_created` datetime(6) NOT NULL COMMENT 'create datetime',`log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='AT transaction mode undo table';CREATE DATABASE `stock`;USE `stock`;DROP TABLE IF EXISTS `t_stock`;CREATE TABLE `t_stock` (`id` int NOT NULL AUTO_INCREMENT,`commodity_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,`count` int DEFAULT '0',PRIMARY KEY (`id`),UNIQUE KEY `commodity_code` (`commodity_code`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;INSERT INTO `t_stock` VALUES (1,'A001','立白洗洁精',7);DROP TABLE IF EXISTS `undo_log`;CREATE TABLE `undo_log` (`branch_id` bigint NOT NULL COMMENT 'branch transaction id',`xid` varchar(128) NOT NULL COMMENT 'global transaction id',`context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',`rollback_info` longblob NOT NULL COMMENT 'rollback info',`log_status` int NOT NULL COMMENT '0:normal status,1:defense status',`log_created` datetime(6) NOT NULL COMMENT 'create datetime',`log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='AT transaction mode undo table';
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Rock18框架之整体框架介绍
- 支持JDK19虚拟线程的web框架,之二:完整开发一个支持虚拟线程的quarkus应用
- 「MySQL高级篇」MySQL之MVCC实现原理&&事务隔离级别的实现
- [Pyhton] SimPy 离散事件模拟框架详解 —— 以一个简单的汽车充电排队模拟为例
- 20 基于SqlSugar的开发框架循序渐进介绍-- 在基于UniApp+Vue的移动端实现多条件查询的处理
- 弹性分布式数据集 RDD及常用算子
- 分布式ID生成方案总结整理
- 「MySQL高级篇」MySQL锁机制 && 事务
- 说说 Redis 事务
- Spring事务传播行为实战