Skip to content

Commit

Permalink
Reset session before returning connection to the pool
Browse files Browse the repository at this point in the history
  • Loading branch information
amihajlovic authored and pashagolub committed Jul 6, 2023
1 parent c49f578 commit 255daad
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/pgengine/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,23 @@ func (pge *PgEngine) getPgxConnConfig() *pgxpool.Config {
pgconn.TypeMap().RegisterType(&pgtype.Type{Name: "timetable.log_type", OID: pge.logTypeOID, Codec: &pgtype.EnumCodec{}})
return err
}
// reset session before returning connection to the pool
// after task completion, if the task is not properly finalized (especially when running in autonomous mode), some objects and/or setting changes will still exist in the session
// if this session is then reused it can cause task failure / unintended side effects
connConfig.AfterRelease = func(pgconn *pgx.Conn) bool {

batch := pgx.Batch{}
batch.Queue("CLOSE ALL;")
batch.Queue("SET SESSION AUTHORIZATION DEFAULT;")
batch.Queue("RESET ALL;")
batch.Queue("SELECT pg_advisory_unlock_all();")
batch.Queue("DISCARD TEMP;")
batch.Queue("UNLISTEN *;")

batchResult := pgconn.SendBatch(context.Background(), &batch)
_, err := batchResult.Exec()
return err != nil
}
if !pge.Start.Debug { //will handle notification in HandleNotifications directly
connConfig.ConnConfig.OnNotification = pge.NotificationHandler
}
Expand Down

0 comments on commit 255daad

Please sign in to comment.