GORM perform single create, update, delete operations in transactions by default to ensure database data integrity.
If you want to treat multiple create, update, delete as one atomic operation, Transaction is made for that.
Transactions
To perform a set of operations within a transaction, the general flow is as below.
funcCreateAnimals(db *gorm.DB)error { return db.Transaction(func(tx *gorm.DB)error { // do some database operations in the transaction (use 'tx' from this point, not 'db') if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil { // return any error will rollback return err }
// do some database operations in the transaction (use 'tx' from this point, not 'db') tx.Create(...)
// ...
// rollback the transaction in case of error tx.Rollback()
// Or commit the transaction tx.Commit()
A Specific Example
funcCreateAnimals(db *gorm.DB)error { // Note the use of tx as the database handle once you are within a transaction tx := db.Begin() deferfunc() { if r := recover(); r != nil { tx.Rollback() } }()