Skip to content

Commit

Permalink
数据库连接池闲置连接数和最大连接数,从配置文件中读取
Browse files Browse the repository at this point in the history
  • Loading branch information
ouqiang committed May 31, 2017
1 parent dc60aae commit e94a7dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/app"
"strconv"
)

type Status int8
Expand Down Expand Up @@ -69,6 +70,17 @@ func CreateDb() *xorm.Engine {
if err != nil {
logger.Fatal("创建xorm引擎失败", err)
}
maxIdleConns, err := strconv.Atoi(config["max_idle_conns"])
maxOpenConns, err := strconv.Atoi(config["max_open_conns"])
if maxIdleConns <= 0 {
maxIdleConns = 30
}
if maxOpenConns <= 0 {
maxOpenConns = 100
}
engine.SetMaxIdleConns(maxIdleConns)
engine.SetMaxOpenConns(maxOpenConns)

if config["prefix"] != "" {
// 设置表前缀
TablePrefix = config["prefix"]
Expand All @@ -81,6 +93,7 @@ func CreateDb() *xorm.Engine {
engine.Logger().SetLevel(core.LOG_DEBUG)
}


return engine
}

Expand Down Expand Up @@ -121,6 +134,8 @@ func getDbConfig() map[string]string {
db["charset"] = app.Setting.Key("db.charset").String()
db["prefix"] = app.Setting.Key("db.prefix").String()
db["engine"] = app.Setting.Key("db.engine").String()
db["max_idle_conns"] = app.Setting.Key("db.max.idle.conns").String()
db["max_open_conns"] = app.Setting.Key("db.max.open.conns").String()

return db
}
1 change: 1 addition & 0 deletions models/task_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (taskLog *TaskLog) Remove(id int) (int64, error) {

func (taskLog *TaskLog) Total(params CommonMap) (int64, error) {
session := Db.NewSession()
defer session.Close()
taskLog.parseWhere(session, params)
return session.Count(taskLog)
}
Expand Down
2 changes: 2 additions & 0 deletions routers/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func writeConfig(form InstallForm) error {
"db.database", form.DbName,
"db.prefix", form.DbTablePrefix,
"db.charset", "utf8",
"db.max.idle.conns", "30",
"db.max.open.conns", "100",
"allow_ips", "",
"app.name", "定时任务管理系统", // 应用名称
"delay.task.enable", "false", // 是否开启延时任务
Expand Down

0 comments on commit e94a7dc

Please sign in to comment.