This commit is contained in:
李子铭 2025-03-20 15:44:36 +08:00
parent bedabb1d17
commit d1b0380c4d
1 changed files with 6 additions and 4 deletions

View File

@ -28,9 +28,11 @@ func db(data *conf.Data_Database) (*gorm.DB, func()) {
mysqlConn, err := sql.Open(data.Driver, data.Source) mysqlConn, err := sql.Open(data.Driver, data.Source)
gormDB, err := gorm.Open( gormDB, err := gorm.Open(
mysql.New(mysql.Config{ mysql.New(
mysql.Config{
Conn: mysqlConn, Conn: mysqlConn,
}), },
),
&gorm.Config{ &gorm.Config{
Logger: logger.Default.LogMode(logger.Info), Logger: logger.Default.LogMode(logger.Info),
}, },
@ -50,7 +52,7 @@ func db(data *conf.Data_Database) (*gorm.DB, func()) {
// SetMaxOpenConns sets the maximum number of openapi connections to the database. // SetMaxOpenConns sets the maximum number of openapi connections to the database.
sqlDB.SetMaxOpenConns(int(data.MaxOpen)) sqlDB.SetMaxOpenConns(int(data.MaxOpen))
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused. // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
sqlDB.SetConnMaxLifetime(time.Hour) sqlDB.SetConnMaxLifetime(1 * time.Hour)
return gormDB, func() { return gormDB, func() {
if mysqlConn != nil { if mysqlConn != nil {