session Transaction Execute sql wrapped in a transaction(abbr as tx), tx will automatic commit if no errors occurred

This commit is contained in:
褚兆前 2025-05-23 09:14:48 +08:00
parent 3b3f99f8a1
commit cb7396945b
1 changed files with 10 additions and 0 deletions

View File

@ -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 {