数据库连接池闲置连接数和最大连接数,从配置文件中读取

pull/21/merge v0.4
ouqiang 2017-05-31 20:39:29 +08:00
parent dc60aae381
commit e94a7dc28a
3 changed files with 18 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/ouqiang/gocron/modules/logger" "github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/app" "github.com/ouqiang/gocron/modules/app"
"strconv"
) )
type Status int8 type Status int8
@ -69,6 +70,17 @@ func CreateDb() *xorm.Engine {
if err != nil { if err != nil {
logger.Fatal("创建xorm引擎失败", err) 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"] != "" { if config["prefix"] != "" {
// 设置表前缀 // 设置表前缀
TablePrefix = config["prefix"] TablePrefix = config["prefix"]
@ -81,6 +93,7 @@ func CreateDb() *xorm.Engine {
engine.Logger().SetLevel(core.LOG_DEBUG) engine.Logger().SetLevel(core.LOG_DEBUG)
} }
return engine return engine
} }
@ -121,6 +134,8 @@ func getDbConfig() map[string]string {
db["charset"] = app.Setting.Key("db.charset").String() db["charset"] = app.Setting.Key("db.charset").String()
db["prefix"] = app.Setting.Key("db.prefix").String() db["prefix"] = app.Setting.Key("db.prefix").String()
db["engine"] = app.Setting.Key("db.engine").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 return db
} }

View File

@ -74,6 +74,7 @@ func (taskLog *TaskLog) Remove(id int) (int64, error) {
func (taskLog *TaskLog) Total(params CommonMap) (int64, error) { func (taskLog *TaskLog) Total(params CommonMap) (int64, error) {
session := Db.NewSession() session := Db.NewSession()
defer session.Close()
taskLog.parseWhere(session, params) taskLog.parseWhere(session, params)
return session.Count(taskLog) return session.Count(taskLog)
} }

View File

@ -111,6 +111,8 @@ func writeConfig(form InstallForm) error {
"db.database", form.DbName, "db.database", form.DbName,
"db.prefix", form.DbTablePrefix, "db.prefix", form.DbTablePrefix,
"db.charset", "utf8", "db.charset", "utf8",
"db.max.idle.conns", "30",
"db.max.open.conns", "100",
"allow_ips", "", "allow_ips", "",
"app.name", "定时任务管理系统", // 应用名称 "app.name", "定时任务管理系统", // 应用名称
"delay.task.enable", "false", // 是否开启延时任务 "delay.task.enable", "false", // 是否开启延时任务