From 9af748f48841d8bd0f9454bb354a306da5d6f906 Mon Sep 17 00:00:00 2001 From: CyJaySong Date: Fri, 17 Jan 2025 13:59:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(database/gdb)=20Begin=E5=BC=80=E5=90=AF?= =?UTF-8?q?=E4=BA=8B=E5=8A=A1=E5=85=81=E8=AE=B8tx.GetCtx()=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E4=BA=8B=E5=8A=A1=E4=BC=A0=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/gdb/gdb_core_transaction.go | 2 ++ database/gdb/gdb_core_underlying.go | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/database/gdb/gdb_core_transaction.go b/database/gdb/gdb_core_transaction.go index 897b179b1d2..1faa73a6205 100644 --- a/database/gdb/gdb_core_transaction.go +++ b/database/gdb/gdb_core_transaction.go @@ -257,12 +257,14 @@ func WithTX(ctx context.Context, tx TX) context.Context { } // Inject transaction object and id into context. ctx = context.WithValue(ctx, transactionKeyForContext(group), tx) + ctx = context.WithValue(ctx, transactionIdForLoggerCtx, tx.GetCtx().Value(transactionIdForLoggerCtx)) return ctx } // WithoutTX removed transaction object from context and returns a new context. func WithoutTX(ctx context.Context, group string) context.Context { ctx = context.WithValue(ctx, transactionKeyForContext(group), nil) + ctx = context.WithValue(ctx, transactionIdForLoggerCtx, nil) return ctx } diff --git a/database/gdb/gdb_core_underlying.go b/database/gdb/gdb_core_underlying.go index 25c60a4baf7..61fd3d0eec8 100644 --- a/database/gdb/gdb_core_underlying.go +++ b/database/gdb/gdb_core_underlying.go @@ -180,14 +180,17 @@ func (c *Core) DoCommit(ctx context.Context, in DoCommitInput) (out DoCommitOutp formattedSql, in.TxOptions.Isolation.String(), in.TxOptions.ReadOnly, ) if sqlTx, err = in.Db.BeginTx(ctx, &in.TxOptions); err == nil { - out.Tx = &TXCore{ + tx := &TXCore{ db: c.db, tx: sqlTx, - ctx: context.WithValue(ctx, transactionIdForLoggerCtx, transactionIdGenerator.Add(1)), + ctx: ctx, master: in.Db, transactionId: guid.S(), cancelFunc: cancelFuncForTimeout, } + tx.ctx = context.WithValue(tx.ctx, transactionIdForLoggerCtx, transactionIdGenerator.Add(1)) + tx.ctx = WithTX(tx.ctx, tx) + out.Tx = tx ctx = out.Tx.GetCtx() } out.RawResult = sqlTx