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) {
|
2020-05-31 18:55:56 +03:00
|
|
|
if tx := db.Begin(); tx.Error == nil {
|
|
|
|
db.Statement.ConnPool = tx.Statement.ConnPool
|
|
|
|
tx.InstanceSet("gorm:started_transaction", true)
|
|
|
|
} else {
|
|
|
|
tx.Error = nil
|
|
|
|
}
|
2020-02-02 14:32:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func CommitOrRollbackTransaction(db *gorm.DB) {
|
2020-05-31 18:55:56 +03:00
|
|
|
if _, ok := db.InstanceGet("gorm:started_transaction"); ok {
|
|
|
|
if db.Error == nil {
|
|
|
|
db.Commit()
|
|
|
|
} else {
|
|
|
|
db.Rollback()
|
|
|
|
}
|
|
|
|
db.Statement.ConnPool = db.ConnPool
|
|
|
|
}
|
2020-02-02 14:32:27 +03:00
|
|
|
}
|