Atomicity, consistency, isolation, persistence
Manually open transactions: db.Begin()-->Tx
a group of sql operations
tx.Exec(), tx.Prepare()... Manually commit commit, rollback
Manually open transactions: db.Begin()-->Tx
a group of sql operations
tx.Exec(), tx.Prepare()... Manually commit commit, rollback
tx.Commit(),tx.Rollback()
package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func main () { /* Transaction: 4 major features: ACID Atomicity: Consistency: Isolation: Persistence: */ //rose-->jack, 2000 yuan db , _ := sql.Open( "mysql" , "root:hanru1314@tcp(127.0.0.1:3306)/my1802?charset=utf8" ) //Open transaction tx , _ := db.Begin() //Provide a set of sql operations var aff1 , aff2 int64 = 0 , 0 result1 , _ := tx.Exec( "UPDATE account SET money=3000 WHERE id=?" , 1 ) result2, _ := tx.Exec("UPDATE account SET money2=2000 WHERE id=?", 2) //fmt.Println(result2) if result1 != nil { aff1, _ = result1.RowsAffected() } if result2 != nil { aff2, _ = result2.RowsAffected(); } fmt.Println(aff1) fmt.Println(aff2) if aff1 == 1 && aff2 == 1 { //Commit the transaction tx.Commit() fmt.Println( "The operation was successful.." ) } else { //rollback tx.Rollback() fmt.Println( "Failed to operate... rollback..." ) } }