Fix master/slave bug (#1601)

fix test

Fix master/slave bug

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1601
This commit is contained in:
Lunny Xiao 2020-03-13 00:42:01 +00:00
parent c2bf301bdb
commit c56c8e122a
3 changed files with 37 additions and 5 deletions

View File

@ -32,7 +32,6 @@ import (
// Commonly, an application only need one engine // Commonly, an application only need one engine
type Engine struct { type Engine struct {
cacherMgr *caches.Manager cacherMgr *caches.Manager
db *core.DB
defaultContext context.Context defaultContext context.Context
dialect dialects.Dialect dialect dialects.Dialect
engineGroup *EngineGroup 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 // SetMaxIdleConns set the max idle connections on pool, default is 2
func (eg *EngineGroup) SetMaxIdleConns(conns int) { 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++ { 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+ // SetMaxOpenConns is only available for go 1.2+
func (eg *EngineGroup) SetMaxOpenConns(conns int) { 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++ { 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)
}