ソースを参照

fix bug when session resue

xormplus 6 年 前
コミット
76107f6894
3 ファイル変更17 行追加3 行削除
  1. 7 0
      engine_group.go
  2. 9 1
      session.go
  3. 1 2
      session_raw.go

+ 7 - 0
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

+ 9 - 1
session.go

@@ -18,6 +18,13 @@ import (
 	"github.com/xormplus/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 {
@@ -56,7 +63,8 @@ type Session struct {
 
 	rollbackSavePointID string
 
-	ctx context.Context
+	ctx         context.Context
+	sessionType sessionType
 
 	err error
 }

+ 1 - 2
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()
@@ -80,7 +80,6 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
 	if err != nil {
 		return nil, err
 	}
-
 	return rows, nil
 }