From 3fb22944153c8842b88404e1ae5f5e966bd186d9 Mon Sep 17 00:00:00 2001 From: "yong.an" Date: Mon, 13 Jul 2020 18:15:22 +0800 Subject: [PATCH 1/2] Revert "accept lunny's suggestion" This reverts commit 57fd6699425bdb97baf6bbeddc3099ffcb4576d8. --- session.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/session.go b/session.go index 709c81f0..7e8b19cb 100644 --- a/session.go +++ b/session.go @@ -100,8 +100,17 @@ func newSessionID() string { } func newSession(engine *Engine) *Session { + var ctx context.Context + var sessionID string + if engine.logSessionID { + sessionID = newSessionID() + ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, sessionID) + } else { + ctx = engine.defaultContext + } + session := &Session{ - ctx: engine.defaultContext, + ctx: ctx, engine: engine, tx: nil, statement: statements.NewStatement( @@ -130,7 +139,7 @@ func newSession(engine *Engine) *Session { sessionType: engineSession, } if engine.logSessionID { - session.ctx = context.WithValue(session.ctx, log.SessionIDKey, session) + session.ctx = context.WithValue(session.ctx, sessionID, session) } return session } From f3ca093478fc3863ba78d6d0478c435126c8871d Mon Sep 17 00:00:00 2001 From: "yong.an" Date: Mon, 13 Jul 2020 18:23:57 +0800 Subject: [PATCH 2/2] add session to context --- log/logger_context.go | 1 + session.go | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/log/logger_context.go b/log/logger_context.go index 6b7252ef..46802576 100644 --- a/log/logger_context.go +++ b/log/logger_context.go @@ -42,6 +42,7 @@ var ( // enumerate all the context keys var ( SessionIDKey = "__xorm_session_id" + SessionKey = "__xorm_session_key" SessionShowSQLKey = "__xorm_show_sql" ) diff --git a/session.go b/session.go index 7e8b19cb..48b3779e 100644 --- a/session.go +++ b/session.go @@ -101,10 +101,8 @@ func newSessionID() string { func newSession(engine *Engine) *Session { var ctx context.Context - var sessionID string if engine.logSessionID { - sessionID = newSessionID() - ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, sessionID) + ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, newSessionID()) } else { ctx = engine.defaultContext } @@ -139,7 +137,7 @@ func newSession(engine *Engine) *Session { sessionType: engineSession, } if engine.logSessionID { - session.ctx = context.WithValue(session.ctx, sessionID, session) + session.ctx = context.WithValue(session.ctx, log.SessionKey, session) } return session }