From dd2f3baa0cdcf5bab1d974876499814db8c8c307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 8 Dec 2022 11:31:52 +0100 Subject: [PATCH] Properly close db cursors after use --- server/db/meta.go | 30 ++++++++++++++++++++++++++++++ server/models/utils.go | 10 ++++++++++ 2 files changed, 40 insertions(+) diff --git a/server/db/meta.go b/server/db/meta.go index 8a3958e..95088a3 100644 --- a/server/db/meta.go +++ b/server/db/meta.go @@ -26,6 +26,11 @@ func (db *Database) ReadSchema(ctx context.Context) (retval int, reterr error) { return 0, nil } + err = r1.Close() + if err != nil { + return 0, err + } + r2, err := db.db.Query(ctx, "SELECT value_int FROM meta WHERE meta_key = :key", sq.PP{"key": "schema"}) if err != nil { return 0, err @@ -49,6 +54,11 @@ func (db *Database) ReadSchema(ctx context.Context) (retval int, reterr error) { return 0, err } + err = r2.Close() + if err != nil { + return 0, err + } + return dbschema, nil } @@ -119,6 +129,11 @@ func (db *Database) ReadMetaString(ctx context.Context, key string) (retval *str return nil, err } + err = r2.Close() + if err != nil { + return nil, err + } + return langext.Ptr(value), nil } @@ -146,6 +161,11 @@ func (db *Database) ReadMetaInt(ctx context.Context, key string) (retval *int64, return nil, err } + err = r2.Close() + if err != nil { + return nil, err + } + return langext.Ptr(value), nil } @@ -173,6 +193,11 @@ func (db *Database) ReadMetaReal(ctx context.Context, key string) (retval *float return nil, err } + err = r2.Close() + if err != nil { + return nil, err + } + return langext.Ptr(value), nil } @@ -200,6 +225,11 @@ func (db *Database) ReadMetaBlob(ctx context.Context, key string) (retval *[]byt return nil, err } + err = r2.Close() + if err != nil { + return nil, err + } + return langext.Ptr(value), nil } diff --git a/server/models/utils.go b/server/models/utils.go index 65d03eb..14e19a3 100644 --- a/server/models/utils.go +++ b/server/models/utils.go @@ -31,10 +31,16 @@ func scanSingle[TData any](rows *sqlx.Rows) (TData, error) { return *new(TData), err } if rows.Next() { + _ = rows.Close() return *new(TData), errors.New("sql returned more than onw row") } + err = rows.Close() + if err != nil { + return *new(TData), err + } return data, nil } else { + _ = rows.Close() return *new(TData), sql.ErrNoRows } } @@ -49,5 +55,9 @@ func scanAll[TData any](rows *sqlx.Rows) ([]TData, error) { } res = append(res, data) } + err := rows.Close() + if err != nil { + return nil, err + } return res, nil }