Skip to content

Commit

Permalink
bugfix: fix missing value of context (apache#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
106umao authored and georgehao committed May 7, 2023
1 parent b80e2e6 commit 07dcc15
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
12 changes: 4 additions & 8 deletions pkg/tm/transaction_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ func WithGlobalTx(ctx context.Context, gc *GtxConfig, business CallbackWithCtx)
ctx = InitSeataContext(ctx)
}

// use new context to process current global transaction.
if IsGlobalTx(ctx) {
ctx = transferTx(ctx)
clearTxConf(ctx)
}

if re = begin(ctx, gc); re != nil {
Expand Down Expand Up @@ -202,10 +201,7 @@ func useExistGtx(ctx context.Context, gc *GtxConfig) {
}
}

// transferTx transfer the gtx into a new ctx from old ctx.
// use it to implement suspend and resume instead of seata java
func transferTx(ctx context.Context) context.Context {
newCtx := InitSeataContext(context.Background())
SetXID(newCtx, GetXID(ctx))
return newCtx
// 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)})
}
19 changes: 15 additions & 4 deletions pkg/tm/transaction_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,22 @@ func TestCommitOrRollback(t *testing.T) {
}
}

func TestTransferTx(t *testing.T) {
func TestClearTxConf(t *testing.T) {
ctx := InitSeataContext(context.Background())
SetXID(ctx, "123456")
newCtx := transferTx(ctx)
assert.Equal(t, GetXID(ctx), GetXID(newCtx))

SetTx(ctx, &GlobalTransaction{
Xid: "123456",
TxName: "MockTxName",
TxStatus: message.GlobalStatusBegin,
TxRole: Launcher,
})

clearTxConf(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))
}

func TestUseExistGtx(t *testing.T) {
Expand Down

0 comments on commit 07dcc15

Please sign in to comment.