fix #1075 and replace #1076

This commit is contained in:
Lunny Xiao 2018-09-17 23:07:27 +08:00
parent 5271caa592
commit 3adc6117d5
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 18 additions and 1 deletions

View File

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

View File

@ -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 {
@ -54,6 +61,8 @@ type Session struct {
err error
ctx context.Context
sessionType sessionType
}
// Clone copy all the session's content and return a new session
@ -86,6 +95,7 @@ func (session *Session) Init() {
session.lastSQLArgs = []interface{}{}
session.ctx = session.engine.defaultContext
session.sessionType = engineSession
}
// Close release the connection from pool

View File

@ -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()