From 91b94878d3786a35d6a989bda963809fc874f19f Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 10 Nov 2022 09:55:08 +0800 Subject: [PATCH] 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 --- database/gdb/gdb_core.go | 17 +++-------------- database/gdb/gdb_core_config.go | 9 ++++++++- database/gdb/gdb_core_ctx.go | 3 ++- version.go | 2 +- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/database/gdb/gdb_core.go b/database/gdb/gdb_core.go index 2ca306318..d07146f3a 100644 --- a/database/gdb/gdb_core.go +++ b/database/gdb/gdb_core.go @@ -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. diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index 2e6c6a7b5..7616a16a2 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -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 } diff --git a/database/gdb/gdb_core_ctx.go b/database/gdb/gdb_core_ctx.go index 2f32bf1e3..3c4c1ab4f 100644 --- a/database/gdb/gdb_core_ctx.go +++ b/database/gdb/gdb_core_ctx.go @@ -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, }) } diff --git a/version.go b/version.go index f6f2a2f52..01b454120 100644 --- a/version.go +++ b/version.go @@ -2,5 +2,5 @@ package gf const ( // VERSION is the current GoFrame version. - VERSION = "v2.2.2" + VERSION = "v2.2.3" )