From e483cf5d764eb09e5f68939e64eee434c56b0cab Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Wed, 15 Nov 2023 22:38:32 +0100 Subject: [PATCH] [-] fix pool after release handler to avoid session termination causing side effects (#615) * [-] fix pool after release handler to avoid session termination causing side effects * [-] deallocate all internal prepared statements before DISCARD * [-] fix `migrateTest` to close opened `pgengine` --- internal/migrator/migrator_test.go | 1 + internal/pgengine/bootstrap.go | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/migrator/migrator_test.go b/internal/migrator/migrator_test.go index d6dd4ee..7bd5c81 100644 --- a/internal/migrator/migrator_test.go +++ b/internal/migrator/migrator_test.go @@ -55,6 +55,7 @@ func migrateTest() error { if err != nil { return err } + defer pge.Finalize() _, _ = pge.ConfigDb.Exec(ctx, "DROP TABLE IF EXISTS foo, bar, baz, migration") db, err := pge.ConfigDb.Acquire(ctx) if err != nil { diff --git a/internal/pgengine/bootstrap.go b/internal/pgengine/bootstrap.go index ccde77f..f0449f1 100644 --- a/internal/pgengine/bootstrap.go +++ b/internal/pgengine/bootstrap.go @@ -184,10 +184,13 @@ func (pge *PgEngine) getPgxConnConfig() *pgxpool.Config { // some objects and/or setting changes will still exist in the session connConfig.AfterRelease = func(pgconn *pgx.Conn) bool { var err error + if pgconn.DeallocateAll(context.Background()) != nil { + return false // destroy the connection in case of error + } if _, err = pgconn.Exec(context.Background(), "DISCARD ALL"); err == nil { _, err = pgconn.Exec(context.Background(), "LISTEN "+quoteIdent(pge.ClientName)) } - return err != nil + return err == nil } if !pge.Start.Debug { //will handle notification in HandleNotifications directly