JDBC & Seata 事务验证

状态:原创 / 转载(请表明来源)

JDBC & Seata 事务验证

服务规划

准备以下本地环境

服务 版本
MySQL 5.7/8.0
Seata 1.4.2
JDBC Master 最新

环境准备

MySQL 建库建表

drop database IF  EXISTS  sbtest1;

drop database IF  EXISTS sbtest2;

create database IF NOT EXISTS sbtest1;

create database IF NOT EXISTS sbtest2;



CREATE TABLE sbtest1.undo_log (id bigint(20) NOT NULL AUTO_INCREMENT,branch_id bigint(20) NOT NULL,xid varchar(100) NOT NULL,context varchar(128) NOT NULL,rollback_info longblob NOT NULL,log_status int(11) NOT NULL,log_created datetime NOT NULL,log_modified datetime NOT NULL,ext varchar(100) DEFAULT NULL,PRIMARY KEY (id),UNIQUE KEY ux_undo_log (xid,branch_id))  AUTO_INCREMENT=1 ;

  

CREATE TABLE sbtest2.undo_log (id bigint(20) NOT NULL AUTO_INCREMENT,branch_id bigint(20) NOT NULL,xid varchar(100) NOT NULL,context varchar(128) NOT NULL,rollback_info longblob NOT NULL,log_status int(11) NOT NULL,log_created datetime NOT NULL,log_modified datetime NOT NULL,ext varchar(100) DEFAULT NULL,PRIMARY KEY (id),UNIQUE KEY ux_undo_log (xid,branch_id))  AUTO_INCREMENT=1 ;

PostgreSQL 建表语句

#### Proxy 的每个 ds 均需创建

CREATE TABLE "public"."undo_log" (

"id" SERIAL NOT NULL primary key,

"branch_id" int8 NOT NULL,

"xid" varchar(100) COLLATE "default" NOT NULL,

"context" varchar(128) COLLATE "default" NOT NULL,

"rollback_info" bytea NOT NULL,

"log_status" int4 NOT NULL,

"log_created" timestamp(6) NOT NULL,

"log_modified" timestamp(6) NOT NULL,

"ext" varchar(100) COLLATE "default" DEFAULT NULL::character varying,

CONSTRAINT "unq_idx_ul_branchId_xid" UNIQUE ("branch_id", "xid")

)

WITH (OIDS=FALSE)

;

ALTER TABLE "public"."undo_log" OWNER TO "postgres";

启动 Seata 服务

docker启动
docker run -d --name seata-server -p 8091:8091 seataio/seata-server:1.4.2

容器命令行及查看日志

$ docker exec -it seata-server sh
$ docker logs -f seata-server
直接部署
https://github.com/seata/seata/archive/refs/tags/v1.4.2.zip

unzip v1.4.2.zip

sh ./bin/seata-server.sh -p 8091 -h 127.0.0.1 -m file

参考部署 Server

JDBC 配置

sharding-databases-tables.yaml

#

# Licensed to the Apache Software Foundation (ASF) under one or more

# contributor license agreements.  See the NOTICE file distributed with

# this work for additional information regarding copyright ownership.

# The ASF licenses this file to You under the Apache License, Version 2.0

# (the "License"); you may not use this file except in compliance with

# the License.  You may obtain a copy of the License at

#

#     http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

#



dataSources:

  ds_0:

    dataSourceClassName: com.zaxxer.hikari.HikariDataSource

    driverClassName: com.mysql.jdbc.Driver

    jdbcUrl: jdbc:mysql://localhost:3306/sbtest1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8

    username: root

    password: root

  ds_1:

    dataSourceClassName: com.zaxxer.hikari.HikariDataSource

    driverClassName: com.mysql.jdbc.Driver

    jdbcUrl: jdbc:mysql://localhost:3306/sbtest2?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8

    username: root

    password: root



rules:

- !SHARDING

  tables:

    t_order: 

      actualDataNodes: ds_${0..1}.t_order_${0..1}

      tableStrategy: 

        standard:

          shardingColumn: order_id

          shardingAlgorithmName: table_inline

      keyGenerateStrategy:

        column: order_id

        keyGeneratorName: snowflake

  

  defaultDatabaseStrategy:

    standard:

      shardingColumn: user_id

      shardingAlgorithmName: database_inline

  defaultTableStrategy:

    none:

  

  shardingAlgorithms:

    database_inline:

      type: INLINE

      props:

        algorithm-expression: ds_${user_id % 2}

    table_inline:

      type: INLINE

      props:

        algorithm-expression: t_order_${order_id % 2}

  

  keyGenerators:

    snowflake:

      type: SNOWFLAKE

      props:

          worker-id: 123



props:

  sql-show: false

验证

下载 ShardingSphere 代码,找到如下方法执行

执行结果如下

2 个赞
京ICP备2021015875号