Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make conn global set time not changed by default number. #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"database/sql"
"fmt"
"gofly/global"
"time"

"gofly/utils/gform" //数据库操作

Expand All @@ -24,9 +23,6 @@ func MyInit(starType interface{}) {
global.App.Log.Info(fmt.Sprintf("数据库连接实例错误: %v", err))
} else {
global.App.Log.Info(fmt.Sprintf("连接数据库成功:%v", starType))
engin.GetExecuteDB().SetMaxIdleConns(10) //连接池最大空闲连接数,不设置, 默认无
engin.GetExecuteDB().SetMaxOpenConns(50) // 连接池最大连接数,不设置, 默认无限
engin.GetExecuteDB().SetConnMaxLifetime(59 * time.Second) //时间比超时时间短
engin.GetQueryDB().Exec("SET @@sql_mode='NO_ENGINE_SUBSTITUTION';")
}
}
Expand Down
9 changes: 5 additions & 4 deletions utils/gform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ type Config struct {
Driver string `json:"driver"` // 驱动: mysql/sqlite3/oracle/mssql/postgres/clickhouse, 如果集群配置了驱动, 这里可以省略
// mysql 示例:
// root:root@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=true
Dsn string `json:"dsn"` // 数据库链接
SetMaxOpenConns int `json:"setMaxOpenConns"` // (连接池)最大打开的连接数,默认值为0表示不限制
SetMaxIdleConns int `json:"setMaxIdleConns"` // (连接池)闲置的连接数, 默认0
Prefix string `json:"prefix"` // 表前缀, 如果集群配置了前缀, 这里可以省略
Dsn string `json:"dsn"` // 数据库链接
SetMaxOpenConns int `json:"setMaxOpenConns"` // (连接池)最大打开的连接数,默认值为0表示不限制
SetMaxIdleConns int `json:"setMaxIdleConns"` // (连接池)闲置的连接数, 默认0
SetConnMaxLifetime int `json:"setConnMaxLifetime"` // (连接池)连接池超时时间, 默认值为0表示和Conn连接对象生命周期一样
Prefix string `json:"prefix"` // 表前缀, 如果集群配置了前缀, 这里可以省略
}

// ConfigCluster ...
Expand Down
4 changes: 4 additions & 0 deletions utils/gform/engin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gform
import (
"database/sql"
"fmt"
"time"
)

// TAGNAME ...
Expand Down Expand Up @@ -221,6 +222,9 @@ func (c *Engin) bootReal(dbConf Config) (db *sql.DB, err error) {
if dbConf.SetMaxIdleConns > 0 {
db.SetMaxIdleConns(dbConf.SetMaxIdleConns)
}
if dbConf.SetConnMaxLifetime > 0 {
db.SetConnMaxLifetime(time.Duration(dbConf.SetConnMaxLifetime) * time.Second)
}

return
}
Expand Down