From 3fae3b652e78419e202b87d64d7e0274e9f3d1c9 Mon Sep 17 00:00:00 2001 From: hookokoko <648646891@qq.com> Date: Fri, 7 Apr 2023 19:23:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=95=E6=9D=A1=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=97=B6=E8=BF=9E=E6=8E=A5=E6=B3=84=E9=9C=B2=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#188)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --------- Co-authored-by: chenhaokun Co-authored-by: Ming Deng --- .CHANGELOG.md | 1 + core.go | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/.CHANGELOG.md b/.CHANGELOG.md index 691762c..8d6ad91 100644 --- a/.CHANGELOG.md +++ b/.CHANGELOG.md @@ -20,6 +20,7 @@ - [eorm: 分库分表: Merger分页实现](https://github.com/ecodeclub/eorm/pull/175) - [eorm: BasicTypeValue重命名](https://github.com/ecodeclub/eorm/pull/177) - [eorm: 分库分表: hash、shadow_hash算法不符合预期](https://github.com/ecodeclub/eorm/pull/174) +- [eorm: 修复单条查询时连接泄露问题](https://github.com/ecodeclub/eorm/pull/188) ## v0.0.1: - [Init Project](https://github.com/ecodeclub/eorm/pull/1) diff --git a/core.go b/core.go index ef5e2af..ce864ec 100644 --- a/core.go +++ b/core.go @@ -36,6 +36,9 @@ func getHandler[T any](ctx context.Context, sess Session, c core, qc *QueryConte if err != nil { return &QueryResult{Err: err} } + defer func() { + _ = rows.Close() + }() if !rows.Next() { return &QueryResult{Err: errs.ErrNoRows} } @@ -53,6 +56,7 @@ func getHandler[T any](ctx context.Context, sess Session, c core, qc *QueryConte if err = val.SetColumns(rows); err != nil { return &QueryResult{Err: err} } + return &QueryResult{Result: tp} } @@ -72,6 +76,9 @@ func getMultiHandler[T any](ctx context.Context, sess Session, c core, qc *Query if err != nil { return &QueryResult{Err: err} } + defer func() { + _ = rows.Close() + }() res := make([]*T, 0, 16) meta := qc.meta if meta == nil { @@ -91,6 +98,7 @@ func getMultiHandler[T any](ctx context.Context, sess Session, c core, qc *Query } res = append(res, tp) } + return &QueryResult{Result: res} }