mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
feat(database/gdb): enable transaction propagation when using tx.GetCtx() after Begin (#4121)
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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(ctx, transactionKeyForContext(tx.db.GetGroup()), tx)
|
||||
tx.ctx = context.WithValue(tx.ctx, transactionIdForLoggerCtx, transactionIdGenerator.Add(1))
|
||||
out.Tx = tx
|
||||
ctx = out.Tx.GetCtx()
|
||||
}
|
||||
out.RawResult = sqlTx
|
||||
|
||||
Reference in New Issue
Block a user