gorm/callbacks/transaction.go

30 lines
579 B
Go
Raw Permalink Normal View History

2020-02-02 14:32:27 +03:00
package callbacks
2020-05-31 18:55:56 +03:00
import (
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
2020-05-31 18:55:56 +03:00
)
2020-02-02 14:32:27 +03:00
func BeginTransaction(db *gorm.DB) {
if !db.Config.SkipDefaultTransaction {
if tx := db.Begin(); tx.Error == nil {
db.Statement.ConnPool = tx.Statement.ConnPool
db.InstanceSet("gorm:started_transaction", true)
} else {
tx.Error = nil
}
2020-05-31 18:55:56 +03:00
}
2020-02-02 14:32:27 +03:00
}
func CommitOrRollbackTransaction(db *gorm.DB) {
if !db.Config.SkipDefaultTransaction {
if _, ok := db.InstanceGet("gorm:started_transaction"); ok {
if db.Error == nil {
db.Commit()
} else {
db.Rollback()
}
db.Statement.ConnPool = db.ConnPool
2020-05-31 18:55:56 +03:00
}
}
2020-02-02 14:32:27 +03:00
}