SQLStore: Close session in withDbSession (#31775) (#32108)

* SQLStore: Close session in withDbSession

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* SQLStore.WithDbSession: Never use session from context

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
(cherry picked from commit 5a0780801b)

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2021-03-19 10:11:26 +01:00
committed by GitHub
co-authored by Arve Knudsen
parent 5d58d0aabb
commit bc463d6a2f
8 changed files with 116 additions and 137 deletions
+5 -13
View File
@@ -46,22 +46,14 @@ func startSession(ctx context.Context, engine *xorm.Engine, beginTran bool) (*DB
return newSess, nil
}
// WithDbSession calls the callback with an session attached to the context.
// WithDbSession calls the callback with a session.
func (ss *SQLStore) WithDbSession(ctx context.Context, callback dbTransactionFunc) error {
sess, err := startSession(ctx, ss.engine, false)
if err != nil {
return err
}
defer sess.Close()
return callback(sess)
return withDbSession(ctx, ss.engine, callback)
}
func withDbSession(ctx context.Context, callback dbTransactionFunc) error {
sess, err := startSession(ctx, x, false)
if err != nil {
return err
}
func withDbSession(ctx context.Context, engine *xorm.Engine, callback dbTransactionFunc) error {
sess := &DBSession{Session: engine.NewSession()}
defer sess.Close()
return callback(sess)
}