Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(database/gdb) Begin开启事务允许tx.GetCtx()用于事务传递 #4121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions database/gdb/gdb_core_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 5 additions & 2 deletions database/gdb/gdb_core_underlying.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading