This is an automated email from the ASF dual-hosted git repository.

zfeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-seata-go.git


The following commit(s) were added to refs/heads/master by this push:
     new b89343b3 fix: The context issue of nested transactions (#848)
b89343b3 is described below

commit b89343b3a5b1c94c6fc18bf4e14a8517f7fcc98d
Author: tanzegen <[email protected]>
AuthorDate: Sat Aug 30 15:10:01 2025 +0800

    fix: The context issue of nested transactions (#848)
    
    fix:In a nested transaction, context of external transaction will be 
overwritten by internal transaction
    
    Co-authored-by: FengZhang <[email protected]>
---
 pkg/tm/transaction_executor.go      | 11 +++++++----
 pkg/tm/transaction_executor_test.go | 15 ++++++++++-----
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/pkg/tm/transaction_executor.go b/pkg/tm/transaction_executor.go
index a9f42b56..93024160 100644
--- a/pkg/tm/transaction_executor.go
+++ b/pkg/tm/transaction_executor.go
@@ -55,7 +55,7 @@ func WithGlobalTx(ctx context.Context, gc *GtxConfig, 
business CallbackWithCtx)
        }
 
        if IsGlobalTx(ctx) {
-               clearTxConf(ctx)
+               ctx = transferTx(ctx)
        }
 
        if re = begin(ctx, gc); re != nil {
@@ -206,7 +206,10 @@ func useExistGtx(ctx context.Context, gc *GtxConfig) {
        }
 }
 
-// clearTxConf When using global transactions in local mode, you need to clear 
tx config to use the propagation of global transactions.
-func clearTxConf(ctx context.Context) {
-       SetTx(ctx, &GlobalTransaction{Xid: GetXID(ctx)})
+// transferTx transfer the gtx into a new ctx from old ctx.
+// use it to implement suspend and resume instead
+func transferTx(ctx context.Context) context.Context {
+       newCtx := InitSeataContext(ctx)
+       SetXID(newCtx, GetXID(ctx))
+       return newCtx
 }
diff --git a/pkg/tm/transaction_executor_test.go 
b/pkg/tm/transaction_executor_test.go
index dafcea91..fec5030a 100644
--- a/pkg/tm/transaction_executor_test.go
+++ b/pkg/tm/transaction_executor_test.go
@@ -318,7 +318,7 @@ func TestCommitOrRollback(t *testing.T) {
        }
 }
 
-func TestClearTxConf(t *testing.T) {
+func TestTransferTx(t *testing.T) {
        ctx := InitSeataContext(context.Background())
 
        SetTx(ctx, &GlobalTransaction{
@@ -328,12 +328,17 @@ func TestClearTxConf(t *testing.T) {
                TxRole:   Launcher,
        })
 
-       clearTxConf(ctx)
+       newCtx := transferTx(ctx)
 
        assert.Equal(t, "123456", GetXID(ctx))
-       assert.Equal(t, UnKnow, *GetTxRole(ctx))
-       assert.Equal(t, message.GlobalStatusUnKnown, *GetTxStatus(ctx))
-       assert.Equal(t, "", GetTxName(ctx))
+       assert.Equal(t, Launcher, *GetTxRole(ctx))
+       assert.Equal(t, message.GlobalStatusBegin, *GetTxStatus(ctx))
+       assert.Equal(t, "MockTxName", GetTxName(ctx))
+
+       assert.Equal(t, "123456", GetXID(newCtx))
+       assert.Equal(t, UnKnow, *GetTxRole(newCtx))
+       assert.Equal(t, message.GlobalStatusUnKnown, *GetTxStatus(newCtx))
+       assert.Equal(t, "", GetTxName(newCtx))
 }
 
 func TestUseExistGtx(t *testing.T) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to