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 {