From cb7396945bb53a1cfb03ac819107750c8e566884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A4=9A=E5=85=86=E5=89=8D?= <1028290810@qq.com> Date: Fri, 23 May 2025 09:14:48 +0800 Subject: [PATCH] session Transaction Execute sql wrapped in a transaction(abbr as tx), tx will automatic commit if no errors occurred --- session_tx.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/session_tx.go b/session_tx.go index 4fa56891..cec11182 100644 --- a/session_tx.go +++ b/session_tx.go @@ -4,6 +4,16 @@ package xorm +// Transaction Execute sql wrapped in a transaction(abbr as tx), tx will automatic commit if no errors occurred +// Prevent the nested opening of multiple transactions by Engine().Transaction +// Prevent the problem that "Begin Begin Commit Rollback" cannot be rolled back +func (session *Session) Transaction(fn func(session *Session) (interface{}, error)) (interface{}, error) { + if session.Tx() != nil { + return fn(session) + } + return session.Engine().Transaction(fn) +} + // Begin a transaction func (session *Session) Begin() error { if session.isAutoCommit {