Browse Source

bugfix: fix missing value of context (#472)

tags/v1.1.0
juzimao GitHub 2 years ago
parent
commit
437f71296d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 12 deletions
  1. +4
    -8
      pkg/tm/transaction_executor.go
  2. +15
    -4
      pkg/tm/transaction_executor_test.go

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

@@ -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 {
@@ -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)})
}

+ 15
- 4
pkg/tm/transaction_executor_test.go View File

@@ -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) {


Loading…
Cancel
Save