Added sub-transaction detection to WrapInTx

This commit is contained in:
Gerhard Gruber 2021-10-27 12:22:54 +02:00
parent 410393c804
commit 221f8963db

View File

@ -523,7 +523,12 @@ func (s *DB) Rollback() *DB {
} }
// WrapInTx wraps a method in a transaction // WrapInTx wraps a method in a transaction
func (s *DB) WrapInTx(f func(tx *DB) (error)) error { func (s *DB) WrapInTx(f func(tx *DB) error) error {
if _, ok := s.db.(*sql.Tx); ok {
// Already in a transaction
return f(s)
} else {
// Lets start a new transaction
tx := s.Begin() tx := s.Begin()
if err := f(tx); err != nil { if err := f(tx); err != nil {
tx.Rollback() tx.Rollback()
@ -532,6 +537,7 @@ func (s *DB) WrapInTx(f func(tx *DB) (error)) error {
tx.Commit() tx.Commit()
return nil return nil
} }
}
// SkipAssocSave disables saving of associations // SkipAssocSave disables saving of associations
func (s *DB) SkipAssocSave() *DB { func (s *DB) SkipAssocSave() *DB {