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:
John Guo
2022-11-10 09:55:08 +08:00
committed by GitHub
parent b000aa3dfe
commit 91b94878d3
4 changed files with 14 additions and 17 deletions

View File

@ -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.

View File

@ -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
}

View File

@ -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,
})
}

View File

@ -2,5 +2,5 @@ package gf
const (
// VERSION is the current GoFrame version.
VERSION = "v2.2.2"
VERSION = "v2.2.3"
)