From 5b282263d87696d58d90842ffca85d79ed0681a6 Mon Sep 17 00:00:00 2001 From: Yan-Fa Li Date: Tue, 9 Jun 2015 14:51:46 -0700 Subject: [PATCH] Update README.md with Transaction example - add a more detailed slightly more realistic example for a transaction. --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 21707a8c..5e790a1c 100644 --- a/README.md +++ b/README.md @@ -868,6 +868,27 @@ tx.Rollback() tx.Commit() ``` +### More Complex Example +``` +func CreateAnimals(db *gorm.DB) err { + tx := db.Begin() + // Note the use of tx as the database handle once you are within a transaction + + if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil { + tx.Rollback() + return err + } + + if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil { + tx.Rollback() + return err + } + + tx.Commit() + return nil +} +``` + ## Scopes ```go