Manager: close database connection on startup errors
When there is an error detected at startup, close the database connection. Before, the connection could be kept open even when an error was returned, causing the write-ahead log files to be kept around. These are now properly integrated into the main database file before exiting.
This commit is contained in:
parent
2bc6c77e49
commit
7f588e6dbc
@ -40,6 +40,18 @@ func OpenDB(ctx context.Context, dsn string) (*DB, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the database connection if there was some error. This prevents
|
||||||
|
// leaking database connections & should remove any write-ahead-log files.
|
||||||
|
closeConnOnReturn := true
|
||||||
|
defer func() {
|
||||||
|
if !closeConnOnReturn {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := db.Close(); err != nil {
|
||||||
|
log.Debug().AnErr("cause", err).Msg("cannot close database connection")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if err := setBusyTimeout(db.gormDB, 5*time.Second); err != nil {
|
if err := setBusyTimeout(db.gormDB, 5*time.Second); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -52,6 +64,7 @@ func OpenDB(ctx context.Context, dsn string) (*DB, error) {
|
|||||||
}
|
}
|
||||||
log.Debug().Msg("database automigration succesful")
|
log.Debug().Msg("database automigration succesful")
|
||||||
|
|
||||||
|
closeConnOnReturn = false
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,6 +87,22 @@ func openDBWithConfig(dsn string, config *gorm.Config) (*DB, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db := DB{
|
||||||
|
gormDB: gormDB,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the database connection if there was some error. This prevents
|
||||||
|
// leaking database connections & should remove any write-ahead-log files.
|
||||||
|
closeConnOnReturn := true
|
||||||
|
defer func() {
|
||||||
|
if !closeConnOnReturn {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := db.Close(); err != nil {
|
||||||
|
log.Debug().AnErr("cause", err).Msg("cannot close database connection")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Use the generic sql.DB interface to set some connection pool options.
|
// Use the generic sql.DB interface to set some connection pool options.
|
||||||
sqlDB, err := gormDB.DB()
|
sqlDB, err := gormDB.DB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -108,10 +137,7 @@ func openDBWithConfig(dsn string, config *gorm.Config) (*DB, error) {
|
|||||||
return nil, fmt.Errorf("enabling SQLite 'normal' sync mode: %w", tx.Error)
|
return nil, fmt.Errorf("enabling SQLite 'normal' sync mode: %w", tx.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
db := DB{
|
closeConnOnReturn = false
|
||||||
gormDB: gormDB,
|
|
||||||
}
|
|
||||||
|
|
||||||
return &db, nil
|
return &db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user