This commit is contained in:
lanruiling 2025-12-03 19:12:50 +08:00
parent 063546bab0
commit 3d0a1e5fb5

View File

@ -30,6 +30,8 @@ type DbConfig struct {
Database string
Charset string
TLS string
MaxIdleConns int
MaxOpenConns int
}
func GetDB(c *gin.Context, dbName ...string) *gorm.DB {
@ -127,6 +129,17 @@ func InitDB(env string, conf DbConfig) {
fmt.Println(err.Error())
os.Exit(-1)
}
if conf.MaxIdleConns != 0 && conf.MaxOpenConns != 0 {
sqlDB, err := db.DB()
if err != nil {
panic(err)
}
// 设置连接池参数
sqlDB.SetMaxIdleConns(conf.MaxIdleConns) // 设置最大空闲连接数为10
sqlDB.SetMaxOpenConns(conf.MaxOpenConns) // 设置最大打开连接数为100
}
DBS[conf.Name] = db
} else {
logger2 := lxzap.NewGormZap(zap.L())
@ -141,6 +154,17 @@ func InitDB(env string, conf DbConfig) {
fmt.Println(err.Error())
os.Exit(-1)
}
if conf.MaxIdleConns != 0 && conf.MaxOpenConns != 0 {
sqlDB, err := db.DB()
if err != nil {
panic(err)
}
// 设置连接池参数
sqlDB.SetMaxIdleConns(conf.MaxIdleConns) // 设置最大空闲连接数为10
sqlDB.SetMaxOpenConns(conf.MaxOpenConns) // 设置最大打开连接数为100
}
DBS[conf.Name] = db
}
DBS[conf.Name] = DBS[conf.Name].Session(&gorm.Session{SkipDefaultTransaction: true})