seata + dynamic-datasource + Springboot + mybatis-plus 集成分布式事务操作

本文介绍Seata分布式事务解决方案的安装与配置流程,包括服务端与客户端的详细设置步骤,以及如何通过Zookeeper进行配置中心的管理。

下载 seata
官方地址:https://seata.io/zh-cn/blog/download.html

数据库脚本

-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAR(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAR(32),
    `transaction_service_group` VARCHAR(32),
    `transaction_name`          VARCHAR(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAR(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(96),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

修改配置文件 file.conf 和 registry.conf配置
file.conf

service {
   
   
  #transaction service group mapping
  vgroupMapping.fbs_tx_group = "seata-server" ##修改
  #only support when registry.type=file, please don't set multiple addresses
  seata-server.grouplist = "192.168.0.2:8089" ##修改
  #degrade, current not support
  enableDegrade = false
  #disable seata
  disableGlobalTransaction = false
}
## transaction log store, only used in seata-server
store {
   
   
  ## store mode: file、db、redis
  mode = "db" ##修改

  ## file store property
  file {
   
   
    ## store location dir
    dir = "sessionStore"
    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    maxBranchSessionSize = 16384
    # globe session size , if exceeded throws exceptions
    maxGlobalSessionSize = 512
    # file buffer size , if exceeded allocate new buffer
    fileWriteBufferCacheSize = 16384
    # when recover batch read size
    sessionReloadReadSize = 100
    # async, sync
    flushDiskMode = async
  }

  ## database store property
  db {
   
    ##修改成自己的地址
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(<
在 `<DynamicTable>` 组件中出现 “Cannot read properties of undefined (reading 'xcUser')” 错误,意味着在尝试访问某个对象的 `xcUser` 属性时,该对象本身是 `undefined`。以下是几种可能的解决方案: #### 1. 使用条件渲染 在渲染 `xcUser` 之前,先检查包含它的对象是否存在。假设数据通过 `props` 传递到 `<DynamicTable>` 组件中: ```jsx import React from 'react'; const DynamicTable = ({ data }) => { return ( <table> <thead> <tr> <th>xcUser</th> </tr> </thead> <tbody> {data && data.map(item => ( <tr key={item.id}> {item.xcUser ? ( <td>{item.xcUser}</td> ) : ( <td>数据缺失</td> )} </tr> ))} </tbody> </table> ); }; export default DynamicTable; ``` #### 2. 默认值设置 在组件中为可能未定义的对象设置默认值。可以使用 `||` 或 `??` 运算符: ```jsx import React from 'react'; const DynamicTable = ({ data }) => { const safeData = data || []; return ( <table> <thead> <tr> <th>xcUser</th> </tr> </thead> <tbody> {safeData.map(item => ( <tr key={item.id}> <td>{item.xcUser || '无数据'}</td> </tr> ))} </tbody> </table> ); }; export default DynamicTable; ``` #### 3. 使用可选链操作符(ES2020+) 可选链操作符 `?.` 可以简化对象属性的访问,避免因对象为 `undefined` 或 `null` 而抛出错误: ```jsx import React from 'react'; const DynamicTable = ({ data }) => { return ( <table> <thead> <tr> <th>xcUser</th> </tr> </thead> <tbody> {data?.map(item => ( <tr key={item.id}> <td>{item?.xcUser || '无数据'}</td> </tr> ))} </tbody> </table> ); }; export default DynamicTable; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值