Browse Source

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 <zfcode@qq.com>
master
tanzegen GitHub 1 month ago
parent
commit
b89343b3a5
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 9 deletions
  1. +7
    -4
      pkg/tm/transaction_executor.go
  2. +10
    -5
      pkg/tm/transaction_executor_test.go

+ 7
- 4
pkg/tm/transaction_executor.go View File

@@ -55,7 +55,7 @@ func WithGlobalTx(ctx context.Context, gc *GtxConfig, business CallbackWithCtx)
} }


if IsGlobalTx(ctx) { if IsGlobalTx(ctx) {
clearTxConf(ctx)
ctx = transferTx(ctx)
} }


if re = begin(ctx, gc); re != nil { 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
} }

+ 10
- 5
pkg/tm/transaction_executor_test.go View File

@@ -318,7 +318,7 @@ func TestCommitOrRollback(t *testing.T) {
} }
} }


func TestClearTxConf(t *testing.T) {
func TestTransferTx(t *testing.T) {
ctx := InitSeataContext(context.Background()) ctx := InitSeataContext(context.Background())


SetTx(ctx, &GlobalTransaction{ SetTx(ctx, &GlobalTransaction{
@@ -328,12 +328,17 @@ func TestClearTxConf(t *testing.T) {
TxRole: Launcher, TxRole: Launcher,
}) })


clearTxConf(ctx)
newCtx := transferTx(ctx)


assert.Equal(t, "123456", GetXID(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) { func TestUseExistGtx(t *testing.T) {


Loading…
Cancel
Save