mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
fix used schema not change in nested transaction when used different schemas (#2279)
* fix used schema not change in nested transaction between different schemas * up Co-authored-by: houseme <housemecn@gmail.com>
This commit is contained in:
@ -23,6 +23,7 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
)
|
||||
|
||||
// GetCore returns the underlying *Core object.
|
||||
@ -124,25 +125,13 @@ func (c *Core) Close(ctx context.Context) (err error) {
|
||||
// Master creates and returns a connection from master node if master-slave configured.
|
||||
// It returns the default connection if master-slave not configured.
|
||||
func (c *Core) Master(schema ...string) (*sql.DB, error) {
|
||||
useSchema := ""
|
||||
if len(schema) > 0 && schema[0] != "" {
|
||||
useSchema = schema[0]
|
||||
} else {
|
||||
useSchema = c.schema
|
||||
}
|
||||
return c.getSqlDb(true, useSchema)
|
||||
return c.getSqlDb(true, gutil.GetOrDefaultStr(c.schema, schema...))
|
||||
}
|
||||
|
||||
// Slave creates and returns a connection from slave node if master-slave configured.
|
||||
// It returns the default connection if master-slave not configured.
|
||||
func (c *Core) Slave(schema ...string) (*sql.DB, error) {
|
||||
useSchema := ""
|
||||
if len(schema) > 0 && schema[0] != "" {
|
||||
useSchema = schema[0]
|
||||
} else {
|
||||
useSchema = c.schema
|
||||
}
|
||||
return c.getSqlDb(false, useSchema)
|
||||
return c.getSqlDb(false, gutil.GetOrDefaultStr(c.schema, schema...))
|
||||
}
|
||||
|
||||
// GetAll queries and returns data records from database.
|
||||
|
||||
@ -210,7 +210,14 @@ func (c *Core) SetMaxConnLifeTime(d time.Duration) {
|
||||
func (c *Core) GetConfig() *ConfigNode {
|
||||
internalData := c.GetInternalCtxDataFromCtx(c.db.GetCtx())
|
||||
if internalData != nil && internalData.ConfigNode != nil {
|
||||
return internalData.ConfigNode
|
||||
// Note:
|
||||
// It so here checks and returns the config from current DB,
|
||||
// if different schemas between current DB and config.Name from context,
|
||||
// for example, in nested transaction scenario, the context is passed all through the logic procedure,
|
||||
// but the config.Name from context may be still the original one from the first transaction object.
|
||||
if c.config.Name == internalData.ConfigNode.Name {
|
||||
return internalData.ConfigNode
|
||||
}
|
||||
}
|
||||
return c.config
|
||||
}
|
||||
|
||||
@ -43,7 +43,8 @@ func (c *Core) InjectInternalCtxData(ctx context.Context) context.Context {
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, internalCtxDataKeyInCtx, &internalCtxData{
|
||||
DB: c.db,
|
||||
DB: c.db,
|
||||
ConfigNode: c.config,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -2,5 +2,5 @@ package gf
|
||||
|
||||
const (
|
||||
// VERSION is the current GoFrame version.
|
||||
VERSION = "v2.2.2"
|
||||
VERSION = "v2.2.3"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user