From 758d723ff0416cf850bd1c0c30c68b3cce72ce59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=BE=99?= Date: Wed, 3 Nov 2021 13:28:27 +0800 Subject: [PATCH] fix connections leak --- finisher_api.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index 497dabd5..21647dca 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -285,11 +285,9 @@ func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) { queryTx := db.Limit(1).Order(clause.OrderByColumn{ Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey}, }) - - if tx = queryTx.Find(dest, conds...); queryTx.RowsAffected == 0 { - if tx.Error != nil { - return tx - } + if tx = queryTx.Find(dest, conds...); tx.Error != nil { + return tx + } else if tx.RowsAffected == 0 { if c, ok := tx.Statement.Clauses["WHERE"]; ok { if where, ok := c.Expression.(clause.Where); ok { tx.assignInterfacesToValue(where.Exprs) @@ -325,7 +323,7 @@ func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) { return tx.Model(dest).Updates(assigns) } - return db + return tx } // Update update attributes with callbacks, refer: https://gorm.io/docs/update.html#Update-Changed-Fields