diff --git a/session.go b/session.go index 761b1415..d82c7cad 100644 --- a/session.go +++ b/session.go @@ -101,13 +101,15 @@ func newSessionID() string { func newSession(engine *Engine) *Session { var ctx context.Context + var sessionID string if engine.logSessionID { - ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, newSessionID()) + sessionID = newSessionID() + ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, sessionID) } else { ctx = engine.defaultContext } - return &Session{ + session := &Session{ ctx: ctx, engine: engine, tx: nil, @@ -136,6 +138,10 @@ func newSession(engine *Engine) *Session { sessionType: engineSession, } + if engine.logSessionID { + ctx = context.WithValue(engine.defaultContext, sessionID, session) + } + return session } // Close release the connection from pool