Browse Source

Fix master/slave bug (#1601)

fix test

Fix master/slave bug

Reviewed-on: #1601
tags/v1.0.0
Lunny Xiao 3 weeks ago
parent
commit
c56c8e122a
3 changed files with 37 additions and 5 deletions
  1. +0
    -1
      engine.go
  2. +4
    -4
      engine_group.go
  3. +33
    -0
      engine_group_test.go

+ 0
- 1
engine.go 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


+ 4
- 4
engine_group.go 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
- 0
engine_group_test.go 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)
}

Loading…
Cancel
Save