Fix master/slave bug #1601

Merged
lunny merged 2 commits from lunny/fix_bug into master 2020-03-13 00:42:11 +00:00
3 changed files with 37 additions and 5 deletions

View File

@ -32,7 +32,6 @@ import (
// Commonly, an application only need one engine
type Engine struct {
cacherMgr *caches.Manager
db *core.DB
defaultContext context.Context
dialect dialects.Dialect
engineGroup *EngineGroup

View File

@ -161,17 +161,17 @@ func (eg *EngineGroup) SetMapper(mapper names.Mapper) {
// SetMaxIdleConns set the max idle connections on pool, default is 2
func (eg *EngineGroup) SetMaxIdleConns(conns int) {
eg.Engine.db.SetMaxIdleConns(conns)
eg.Engine.dialect.DB().SetMaxIdleConns(conns)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].db.SetMaxIdleConns(conns)
eg.slaves[i].dialect.DB().SetMaxIdleConns(conns)
}
}
// SetMaxOpenConns is only available for go 1.2+
func (eg *EngineGroup) SetMaxOpenConns(conns int) {
eg.Engine.db.SetMaxOpenConns(conns)
eg.Engine.dialect.DB().SetMaxOpenConns(conns)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].db.SetMaxOpenConns(conns)
eg.slaves[i].dialect.DB().SetMaxOpenConns(conns)
}
}

33
engine_group_test.go Normal file
View File

@ -0,0 +1,33 @@
// Copyright 2020 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"testing"
"github.com/stretchr/testify/assert"
"xorm.io/xorm/log"
"xorm.io/xorm/schemas"
)
func TestEngineGroup(t *testing.T) {
assert.NoError(t, prepareEngine())
master := testEngine.(*Engine)
if master.Dialect().URI().DBType == schemas.SQLITE {
t.Skip()
return
}
eg, err := NewEngineGroup(master, []*Engine{master})
assert.NoError(t, err)
eg.SetMaxIdleConns(10)
eg.SetMaxOpenConns(100)
eg.SetTableMapper(master.GetTableMapper())
eg.SetColumnMapper(master.GetColumnMapper())
eg.SetLogLevel(log.LOG_INFO)
eg.ShowSQL(true)
}