From c7eb06599509dc7071540f9f6749bc6d06d96c7d Mon Sep 17 00:00:00 2001 From: zenix Date: Mon, 6 Jun 2022 18:57:24 +0900 Subject: [PATCH] fix: close / rollback queries/transactions on error --- pkg/service/backtest.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/service/backtest.go b/pkg/service/backtest.go index 9d445525f..f4f80f774 100644 --- a/pkg/service/backtest.go +++ b/pkg/service/backtest.go @@ -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 }