From 6fe3ab6075e194b550aa17e0c71f5b34c87620a3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 20 Jan 2019 12:45:38 +0800 Subject: [PATCH] fix #1075 and replace #1076 (#1101) * fix #1075 and replace #1076 * fix bug when session resue --- engine_group.go | 7 +++++++ session.go | 11 +++++++++-- session_raw.go | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/engine_group.go b/engine_group.go index 5eee3e61..6796075e 100644 --- a/engine_group.go +++ b/engine_group.go @@ -74,6 +74,13 @@ func (eg *EngineGroup) Close() error { return nil } +// NewSession returned a group session +func (eg *EngineGroup) NewSession() *Session { + sess := eg.Engine.NewSession() + sess.sessionType = groupSession + return sess +} + // Master returns the master engine func (eg *EngineGroup) Master() *Engine { return eg.Engine diff --git a/session.go b/session.go index e3f7b989..2307a414 100644 --- a/session.go +++ b/session.go @@ -18,6 +18,13 @@ import ( "github.com/go-xorm/core" ) +type sessionType int + +const ( + engineSession sessionType = iota + groupSession +) + // Session keep a pointer to sql.DB and provides all execution of all // kind of database operations. type Session struct { @@ -52,8 +59,8 @@ type Session struct { lastSQL string lastSQLArgs []interface{} - err error - ctx context.Context + ctx context.Context + sessionType sessionType } // Clone copy all the session's content and return a new session diff --git a/session_raw.go b/session_raw.go index 23ef0a16..c2556365 100644 --- a/session_raw.go +++ b/session_raw.go @@ -49,7 +49,7 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row if session.isAutoCommit { var db *core.DB - if session.engine.engineGroup != nil { + if session.sessionType == groupSession { db = session.engine.engineGroup.Slave().DB() } else { db = session.DB()