Use defer to close rows to avoid scan panic leak rows

This commit is contained in:
Jinzhu 2022-03-29 18:48:06 +08:00
parent 9dd6ed9c65
commit ea8509b777
2 changed files with 6 additions and 2 deletions

View File

@ -84,8 +84,10 @@ func Create(config *Config) func(db *gorm.DB) {
db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...,
)
if db.AddError(err) == nil {
gorm.Scan(rows, db, mode)
defer func() {
db.AddError(rows.Close())
}()
gorm.Scan(rows, db, mode)
}
return

View File

@ -20,8 +20,10 @@ func Query(db *gorm.DB) {
db.AddError(err)
return
}
gorm.Scan(rows, db, 0)
defer func() {
db.AddError(rows.Close())
}()
gorm.Scan(rows, db, 0)
}
}
}