Merge pull request #679 from zenixls2/fix/close_rows

fix: close / rollback queries/transactions on error
This commit is contained in:
Yo-An Lin 2022-06-06 18:10:12 +08:00 committed by GitHub
commit 6c6d5a27a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -125,6 +125,7 @@ func (s *BacktestService) QueryKLine(ex types.ExchangeName, symbol string, inter
"interval": interval,
"symbol": symbol,
})
defer rows.Close()
if err != nil {
return nil, errors.Wrap(err, "query kline error")
@ -134,8 +135,6 @@ func (s *BacktestService) QueryKLine(ex types.ExchangeName, symbol string, inter
return nil, rows.Err()
}
defer rows.Close()
if rows.Next() {
var kline types.KLine
err = rows.StructScan(&kline)
@ -263,6 +262,7 @@ func (s *BacktestService) scanRowsCh(rows *sqlx.Rows) (chan types.KLine, chan er
}
func (s *BacktestService) scanRows(rows *sqlx.Rows) (klines []types.KLine, err error) {
defer rows.Close()
for rows.Next() {
var kline types.KLine
if err := rows.StructScan(&kline); err != nil {
@ -308,6 +308,9 @@ func (s *BacktestService) BatchInsert(kline []types.KLine) error {
tx := s.DB.MustBegin()
if _, err := tx.NamedExec(sql, kline); err != nil {
if e := tx.Rollback(); e != nil {
log.WithError(e).Fatalf("cannot rollback insertion %v", err)
}
return err
}
return tx.Commit()
@ -389,6 +392,7 @@ func (s *BacktestService) FindMissingTimeRanges(ctx context.Context, ex types.Ex
}
rows, err := s.DB.QueryContext(ctx, sql, args...)
defer rows.Close()
if err != nil {
return nil, err
}