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:
Sybren A. Stüvel 2023-07-07 15:48:08 +02:00
parent 2bc6c77e49
commit 7f588e6dbc

View File

@ -40,6 +40,18 @@ func OpenDB(ctx context.Context, dsn string) (*DB, error) {
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 {
return nil, err
}
@ -52,6 +64,7 @@ func OpenDB(ctx context.Context, dsn string) (*DB, error) {
}
log.Debug().Msg("database automigration succesful")
closeConnOnReturn = false
return db, nil
}
@ -74,6 +87,22 @@ func openDBWithConfig(dsn string, config *gorm.Config) (*DB, error) {
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.
sqlDB, err := gormDB.DB()
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)
}
db := DB{
gormDB: gormDB,
}
closeConnOnReturn = false
return &db, nil
}