Skip to content

Commit

Permalink
feat(database/gdb) Begin开启事务允许tx.GetCtx()用于事务传递
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjaysong committed Jan 22, 2025
1 parent e0f7348 commit 267b6a8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contrib/drivers/mysql/mysql_z_unit_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
TestPartitionDB = "test3"
TableNamePrefix1 = "gf_"
TestDbUser = "root"
TestDbPass = "12345678"
TestDbPass = "wq1997qw"
CreateTime = "2018-10-24 10:00:00"
)

Expand Down
41 changes: 41 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1708,3 +1708,44 @@ func Test_Transaction_Isolation(t *testing.T) {
t.AssertNil(err)
})
}

func Test_Transaction_Spread(t *testing.T) {
table := createTable()
defer dropTable(table)

db.SetDebug(true)
defer db.SetDebug(false)

gtest.C(t, func(t *gtest.T) {
var (
err error
ctx = context.TODO()
)
tx, err := db.Begin(ctx)
t.AssertNil(err)
err = db.Transaction(tx.GetCtx(), func(ctx context.Context, tx gdb.TX) error {
_, err = db.Model(table).Ctx(ctx).Data(g.Map{
"id": 1,
"passport": "USER_1",
"password": "PASS_1",
"nickname": "NAME_1",
"create_time": gtime.Now().String(),
}).Insert()
return err
})
t.AssertNil(err)

all, err := tx.Model(table).All()
t.AssertNil(err)

t.Assert(len(all), 1)
t.Assert(all[0]["id"], 1)

err = tx.Rollback()
t.AssertNil(err)

all, err = db.Ctx(ctx).Model(table).All()
t.AssertNil(err)
t.Assert(len(all), 0)
})
}
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(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
Expand Down

0 comments on commit 267b6a8

Please sign in to comment.