apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.81k stars 6.7k forks source link

How to use BASE transaction with seata? #31703

Closed WXingSong closed 2 months ago

WXingSong commented 3 months ago

version:

Sharding-jdbc: 5.5.0 Seata: 2.0.0 druid: 1.2.21 Springboot 3.2.1 spring cloud alibaba 2022.0.0.0

I also have the same problem with #31715

@AutoConfiguration
public class SeataFeignInterceptorConfiguration implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SeataFeignInterceptor()).addPathPatterns("/**");
    }

    public static class SeataFeignInterceptor implements HandlerInterceptor {

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            String xid = RootContext.getXID();
            if (xid == null) {
                xid = request.getHeader(RootContext.KEY_XID);
            }

            if (!StringUtils.isBlank(xid) && SeataTransactionHolder.get() == null) {
                RootContext.bind(xid);
                SeataTransactionHolder.set(GlobalTransactionContext.getCurrentOrCreate());
            }

            return true;
        }
    }
}

This is the execution result

SeataTestServiceImplA

2024-06-28 11:28:19.814 |  INFO 19460 | http-nio-48081-exec-1 [TID: N/A] c.i.y.f.a.c.i.ApiAccessLogInterceptor    | [afterCompletion][完成请求 URL(/admin-api/system/notify-message/get-unread-count) 耗时(175 ms)]
2024-06-28 11:28:22.376 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] c.i.y.f.a.c.i.ApiAccessLogInterceptor    | [preHandle][开始请求 URL(/admin-api/system/seataTest/saveSeataTest) 无参数]
2024-06-28 11:28:22.394 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] io.seata.tm.TransactionManagerHolder     | TransactionManager Singleton io.seata.tm.DefaultTransactionManager@527d7e7e
2024-06-28 11:28:22.405 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] i.seata.tm.api.DefaultGlobalTransaction  | Begin new global transaction [192.168.110.43:8091:1108421793403109164]
2024-06-28 11:28:22.413 | DEBUG 19460 | http-nio-48081-exec-2 [TID: N/A] c.i.y.m.s.d.m.s.SeataTset1Mapper.insert  | ==>  Preparing: INSERT INTO seata_test_1 (name, age, create_time, tenant_id) VALUES (?, ?, ?, 1)
2024-06-28 11:28:22.430 | DEBUG 19460 | http-nio-48081-exec-2 [TID: N/A] c.i.y.m.s.d.m.s.SeataTset1Mapper.insert  | ==> Parameters: 张三(String), 13(Integer), 2024-06-28T11:28:22.406878100(LocalDateTime)
2024-06-28 11:28:22.430 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] ShardingSphere-SQL                       | Logic SQL: INSERT INTO seata_test_1 (name, age, create_time, tenant_id) VALUES (?, ?, ?, 1)
2024-06-28 11:28:22.430 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] ShardingSphere-SQL                       | Actual SQL: master ::: INSERT INTO seata_test_1 (name, age, create_time, tenant_id) VALUES (?, ?, ?, 1) ::: [张三, 13, 2024-06-28T11:28:22.406878100]
2024-06-28 11:28:22.602 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] c.a.n.client.config.impl.ClientWorker    | [fixed-dev-127.0.0.1_8848] [subscribe] client.rm.lock.retryInterval+SEATA_GROUP+dev
2024-06-28 11:28:22.602 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] c.a.nacos.client.config.impl.CacheData   | [fixed-dev-127.0.0.1_8848] [add-listener] ok, tenant=dev, dataId=client.rm.lock.retryInterval, group=SEATA_GROUP, cnt=1
2024-06-28 11:28:22.602 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] c.a.nacos.client.config.impl.CacheData   | [fixed-dev-127.0.0.1_8848] [add-listener] ok, tenant=dev, dataId=client.rm.lock.retryInterval, group=SEATA_GROUP, cnt=2
2024-06-28 11:28:22.602 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] c.a.n.client.config.impl.ClientWorker    | [fixed-dev-127.0.0.1_8848] [subscribe] client.rm.lock.retryTimes+SEATA_GROUP+dev
2024-06-28 11:28:22.602 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] c.a.nacos.client.config.impl.CacheData   | [fixed-dev-127.0.0.1_8848] [add-listener] ok, tenant=dev, dataId=client.rm.lock.retryTimes, group=SEATA_GROUP, cnt=1
2024-06-28 11:28:22.602 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] c.a.nacos.client.config.impl.CacheData   | [fixed-dev-127.0.0.1_8848] [add-listener] ok, tenant=dev, dataId=client.rm.lock.retryTimes, group=SEATA_GROUP, cnt=2
2024-06-28 11:28:22.755 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] io.seata.rm.AbstractResourceManager      | branch register success, xid:192.168.110.43:8091:1108421793403109164, branchId:1108421793403109168, lockKeys:seata_test_1:224
2024-06-28 11:28:22.782 |  WARN 19460 | http-nio-48081-exec-2 [TID: N/A] ServiceLoader$InnerEnhancedServiceLoader | Load [io.seata.rm.datasource.undo.parser.ProtostuffUndoLogParser] class fail: io/protostuff/runtime/IdStrategy
2024-06-28 11:28:22.827 | DEBUG 19460 | http-nio-48081-exec-2 [TID: N/A] c.i.y.m.s.d.m.s.SeataTset1Mapper.insert  | <==    Updates: 1
2024-06-28 11:28:22.843 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] com.alibaba.nacos.client.naming          | [SUBSCRIBE-SERVICE] service:infra-server, group:DEFAULT_GROUP, clusters: 
2024-06-28 11:28:22.845 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] com.alibaba.nacos.client.naming          | init new ips(1) service: DEFAULT_GROUP@@infra-server -> [{"instanceId":"192.168.110.43#48082#DEFAULT#DEFAULT_GROUP@@infra-server","ip":"192.168.110.43","port":48082,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@infra-server","metadata":{"management.endpoints.web.base-path":"/actuator","preserved.register.source":"SPRING_CLOUD"},"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000,"ipDeleteTimeout":30000}]
2024-06-28 11:28:22.845 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] com.alibaba.nacos.client.naming          | current ips:(1) service: DEFAULT_GROUP@@infra-server -> [{"instanceId":"192.168.110.43#48082#DEFAULT#DEFAULT_GROUP@@infra-server","ip":"192.168.110.43","port":48082,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@infra-server","metadata":{"management.endpoints.web.base-path":"/actuator","preserved.register.source":"SPRING_CLOUD"},"instanceHeartBeatTimeOut":15000,"instanceHeartBeatInterval":5000,"ipDeleteTimeout":30000}]
2024-06-28 11:28:23.443 |  INFO 19460 | nacos-grpc-client-executor-192.168.110.43-10 [TID: N/A] com.alibaba.nacos.common.remote.client   | [46822bbd-0793-44b0-8ef1-6d486bd86d8c] Receive server push request, request = NotifySubscriberRequest, requestId = 2453
2024-06-28 11:28:23.444 |  INFO 19460 | nacos-grpc-client-executor-192.168.110.43-10 [TID: N/A] com.alibaba.nacos.common.remote.client   | [46822bbd-0793-44b0-8ef1-6d486bd86d8c] Ack server push request, request = NotifySubscriberRequest, requestId = 2453
2024-06-28 11:28:26.255 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] i.seata.tm.api.DefaultGlobalTransaction  | transaction 192.168.110.43:8091:1108421793403109164 will be rollback
2024-06-28 11:28:26.262 |  INFO 19460 | rpcDispatch_RMROLE_1_1_32 [TID: N/A] i.s.c.r.p.c.RmBranchRollbackProcessor    | rm handle branch rollback process:BranchRollbackRequest{xid='192.168.110.43:8091:1108421793403109164', branchId=1108421793403109168, branchType=AT, resourceId='jdbc:mysql://192.168.110.177:33067/runxi', applicationData='null'}
2024-06-28 11:28:26.263 |  INFO 19460 | rpcDispatch_RMROLE_1_1_32 [TID: N/A] io.seata.rm.AbstractRMHandler            | Branch Rollbacking: 192.168.110.43:8091:1108421793403109164 1108421793403109168 jdbc:mysql://192.168.110.177:33067/runxi
2024-06-28 11:28:26.409 |  INFO 19460 | rpcDispatch_RMROLE_1_1_32 [TID: N/A] i.s.r.d.undo.AbstractUndoLogManager      | xid 192.168.110.43:8091:1108421793403109164 branch 1108421793403109168, undo_log deleted with GlobalFinished
2024-06-28 11:28:26.422 |  INFO 19460 | rpcDispatch_RMROLE_1_1_32 [TID: N/A] i.seata.rm.datasource.DataSourceManager  | branch rollback success, xid:192.168.110.43:8091:1108421793403109164, branchId:1108421793403109168
2024-06-28 11:28:26.423 |  INFO 19460 | rpcDispatch_RMROLE_1_1_32 [TID: N/A] io.seata.rm.AbstractRMHandler            | Branch Rollbacked result: PhaseTwo_Rollbacked
2024-06-28 11:28:26.430 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] i.seata.tm.api.DefaultGlobalTransaction  | transaction end, xid = 192.168.110.43:8091:1108421793403109164
2024-06-28 11:28:26.430 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] i.seata.tm.api.DefaultGlobalTransaction  | [192.168.110.43:8091:1108421793403109164] rollback status: Rollbacked
2024-06-28 11:28:26.434 |  INFO 19460 | http-nio-48081-exec-2 [TID: N/A] c.i.y.f.w.c.h.GlobalExceptionHandler     | [serviceExceptionHandler]

SeataTestServiceImplB

2024-06-28 14:05:53.052 |  INFO 28996 | http-nio-48082-exec-3 [TID: N/A] c.i.y.f.a.c.i.ApiAccessLogInterceptor    | [preHandle][开始请求 URL(/rpc-api/infra/api-seataTest/saveSeataTest3) 无参数]
===================RootContext.getXID():192.168.110.43:8091:1108421793403109398
2024-06-28 14:05:53.057 | DEBUG 28996 | http-nio-48082-exec-3 [TID: N/A] c.i.y.m.i.d.m.s.SeataTest3Mapper.insert  | ==>  Preparing: INSERT INTO seata_test_3 (subject, achievement, create_time, tenant_id) VALUES (?, ?, ?, 1)
2024-06-28 14:05:53.064 | DEBUG 28996 | http-nio-48082-exec-3 [TID: N/A] c.i.y.m.i.d.m.s.SeataTest3Mapper.insert  | ==> Parameters: 数学(String), 90(Integer), 2024-06-28T14:05:53.055821300(LocalDateTime)
2024-06-28 14:05:53.065 |  INFO 28996 | http-nio-48082-exec-3 [TID: N/A] ShardingSphere-SQL                       | Logic SQL: INSERT INTO seata_test_3 (subject, achievement, create_time, tenant_id) VALUES (?, ?, ?, 1)
2024-06-28 14:05:53.065 |  INFO 28996 | http-nio-48082-exec-3 [TID: N/A] ShardingSphere-SQL                       | Actual SQL: master ::: INSERT INTO seata_test_3 (subject, achievement, create_time, tenant_id) VALUES (?, ?, ?, 1) ::: [数学, 90, 2024-06-28T14:05:53.055821300]
2024-06-28 14:05:53.204 | DEBUG 28996 | http-nio-48082-exec-3 [TID: N/A] c.i.y.m.i.d.m.s.SeataTest3Mapper.insert  | <==    Updates: 1
2024-06-28 14:05:53.206 |  INFO 28996 | http-nio-48082-exec-3 [TID: N/A] c.i.y.f.a.c.i.ApiAccessLogInterceptor    | [afterCompletion][完成请求 URL(/rpc-api/infra/api-seataTest/saveSeataTest3) 耗时(154 ms)]

I found that the connection used by SeataTestImplB when executing SQL is not Seata's ConnectionProxy, and branch transactions were not registered using ConnectionProxy#processGlobalTransactionCommit(). But I'm not sure if this is the reason why distributed transactions cannot be rolled back.

1719555129815 1719555179001

linghengqian no time check it😂. I really need your help very much @TherChenYang .Thanks~

WXingSong commented 2 months ago

This is my demo https://github.com/WXingSong/sharding-seata-simple-demo.git @linghengqian The same problem with #31715. Perform unit testing on the Seata integration module of ShardingSphere in the multi-microservice scenario.