Skip to main content

Sessions

A session is a context on the same connections, with a tracker of its own. Call ant.Open once at startup, and take a session per request.

One per request​

func handle(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

session := ant.Session(db)

user, err := session.Users.Find(ctx, id)
if err != nil {
http.Error(w, "not found", http.StatusNotFound)

return
}

user.Age++

err = session.SaveChanges(ctx)
}

Each session saves only its own changes, and many can run at once. Observers registered on the root are shared.

Close the root only

A session shares the root's pool, so closing a session closes it for everyone. Close the context you got from Open, and only that one.

Options​

A session takes its settings from the root, and options change them for that session alone.

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn, ant.WithTracking(false))

session := ant.Session(db, ant.WithTracking(true))

A session on a tracking-off root inherits that, so SaveChanges would have nothing to save. Pass WithTracking(true) to the sessions that write.