fix #1075 使用EngineGroup实现一主多从集群,无法从Master执行Sql查询,并且总是从Slave查询。
This commit is contained in:
parent
e88d320934
commit
bfe42aec25
|
@ -308,6 +308,13 @@ func (engine *Engine) NewSession() *Session {
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) NewMasterSession() *Session {
|
||||||
|
session := &Session{engine: engine}
|
||||||
|
session.Init()
|
||||||
|
session.isMasterOwn=true
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
|
||||||
// Close the engine
|
// Close the engine
|
||||||
func (engine *Engine) Close() error {
|
func (engine *Engine) Close() error {
|
||||||
return engine.db.Close()
|
return engine.db.Close()
|
||||||
|
|
|
@ -27,6 +27,7 @@ type Session struct {
|
||||||
isAutoCommit bool
|
isAutoCommit bool
|
||||||
isCommitedOrRollbacked bool
|
isCommitedOrRollbacked bool
|
||||||
isAutoClose bool
|
isAutoClose bool
|
||||||
|
isMasterOwn bool
|
||||||
|
|
||||||
// Automatically reset the statement after operations that execute a SQL
|
// Automatically reset the statement after operations that execute a SQL
|
||||||
// query such as Count(), Find(), Get(), ...
|
// query such as Count(), Find(), Get(), ...
|
||||||
|
@ -69,6 +70,7 @@ func (session *Session) Init() {
|
||||||
session.isAutoClose = false
|
session.isAutoClose = false
|
||||||
session.autoResetStatement = true
|
session.autoResetStatement = true
|
||||||
session.prepareStmt = false
|
session.prepareStmt = false
|
||||||
|
session.isMasterOwn = false
|
||||||
|
|
||||||
// !nashtsai! is lazy init better?
|
// !nashtsai! is lazy init better?
|
||||||
session.afterInsertBeans = make(map[interface{}]*[]func(interface{}), 0)
|
session.afterInsertBeans = make(map[interface{}]*[]func(interface{}), 0)
|
||||||
|
|
|
@ -49,12 +49,18 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
|
||||||
|
|
||||||
if session.isAutoCommit {
|
if session.isAutoCommit {
|
||||||
var db *core.DB
|
var db *core.DB
|
||||||
if session.engine.engineGroup != nil {
|
|
||||||
db = session.engine.engineGroup.Slave().DB()
|
if session.isMasterOwn{
|
||||||
} else {
|
|
||||||
db = session.DB()
|
db = session.DB()
|
||||||
|
}else{
|
||||||
|
if session.engine.engineGroup != nil {
|
||||||
|
db = session.engine.engineGroup.Slave().DB()
|
||||||
|
} else {
|
||||||
|
db = session.DB()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if session.prepareStmt {
|
if session.prepareStmt {
|
||||||
// don't clear stmt since session will cache them
|
// don't clear stmt since session will cache them
|
||||||
stmt, err := session.doPrepare(db, sqlStr)
|
stmt, err := session.doPrepare(db, sqlStr)
|
||||||
|
|
Loading…
Reference in New Issue