介绍

:::tips AT模式中的快照生成、回滚等动作都是由框架自动完成,没有任何代码侵入,因此实现非常简单,但是使用AT模式需要准备两张表(一张表记录全局锁,一张表记录数据快照) :::

导入数据库表

:::tips 在Seata-TC服务关联的数据库中运行下面的SQL语句,创建记录全局锁的表(lock_table) :::

  1. SET NAMES utf8mb4;
  2. SET FOREIGN_KEY_CHECKS = 0;
  3. -- ----------------------------
  4. -- Table structure for lock_table
  5. -- ----------------------------
  6. DROP TABLE IF EXISTS `lock_table`;
  7. CREATE TABLE `lock_table` (
  8. `row_key` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  9. `xid` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  10. `transaction_id` bigint NULL DEFAULT NULL,
  11. `branch_id` bigint NOT NULL,
  12. `resource_id` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  13. `table_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  14. `pk` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  15. `gmt_create` datetime NULL DEFAULT NULL,
  16. `gmt_modified` datetime NULL DEFAULT NULL,
  17. PRIMARY KEY (`row_key`) USING BTREE,
  18. INDEX `idx_branch_id`(`branch_id` ASC) USING BTREE
  19. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
  20. SET FOREIGN_KEY_CHECKS = 1;

:::tips 在每个参与事务的微服务关联的数据库中运行下面的SQL语句,创建记录数据快照的表(undo_log) :::

  1. SET NAMES utf8mb4;
  2. SET FOREIGN_KEY_CHECKS = 0;
  3. -- ----------------------------
  4. -- Table structure for undo_log
  5. -- ----------------------------
  6. DROP TABLE IF EXISTS `undo_log`;
  7. CREATE TABLE `undo_log` (
  8. `branch_id` bigint NOT NULL COMMENT 'branch transaction id',
  9. `xid` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'global transaction id',
  10. `context` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'undo_log context,such as serialization',
  11. `rollback_info` longblob NOT NULL COMMENT 'rollback info',
  12. `log_status` int NOT NULL COMMENT '0:normal status,1:defense status',
  13. `log_created` datetime(6) NOT NULL COMMENT 'create datetime',
  14. `log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',
  15. UNIQUE INDEX `ux_undo_log`(`xid` ASC, `branch_id` ASC) USING BTREE
  16. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = 'AT transaction mode undo table' ROW_FORMAT = Dynamic;
  17. SET FOREIGN_KEY_CHECKS = 1;

添加配置

:::tips 在每个参与事务的微服务的配置文件中添加配置来开启AT模式 :::

  1. seata:
  2. #默认就是AT模式,无需配置
  3. data-source-proxy-mode: AT