diff --git a/models/model.go b/models/model.go index 00fc092..768866b 100644 --- a/models/model.go +++ b/models/model.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/ouqiang/gocron/modules/logger" "github.com/ouqiang/gocron/modules/app" + "strconv" ) type Status int8 @@ -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"] @@ -81,6 +93,7 @@ func CreateDb() *xorm.Engine { engine.Logger().SetLevel(core.LOG_DEBUG) } + return engine } @@ -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 } \ No newline at end of file diff --git a/models/task_log.go b/models/task_log.go index e0b6f30..47db18c 100644 --- a/models/task_log.go +++ b/models/task_log.go @@ -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) } diff --git a/routers/install/install.go b/routers/install/install.go index 091cb67..6ac33b7 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -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", // 是否开启延时任务