这期内容当中小编将会给大家带来有关怎么在golang中实现mysql数据库事务的提交与回滚,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
golang适合做什么
golang可以做服务器端开发,但golang很适合做日志处理、数据打包、虚拟机处理、数据库代理等工作。在网络编程方面,它还广泛应用于web应用、API应用等领域。
用到的库有:
"github.com/jmoiron/sqlx" _ "github.com/go-sql-driver/mysql"
事务(Transactions)
事务操作是通过三个方法实现:
Begin():开启事务
Commit():提交事务(执行sql)
Rollback():回滚
举例:
在事物里操作MySQL任意一步操作出错,都需要Rollback()回滚。
package main import ( "fmt" "github.com/alecthomas/log4go" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) var Db *sqlx.DB func init() { db,err:=sqlx.Open("mysql","TigerwolfC:123456@tcp(127.0.0.1:3306)/test?charset=utf8") if err != nil { fmt.Println("open mysql failed,", err) return } Db = db } func main() { mysqlTest() } func mysqlTest() error{ tx, err := Db.Begin() if err != nil { log4go.Error("open mysql database fail", err) return err } result, err := tx.Exec("INSERT INTO userinfo (username, password,department,email) VALUES (?, ?,?,?)","cici","33333","it","TigerwolfC@163.com") if err != nil{ fmt.Println("insert failed,error: ", err) tx.Rollback() return err } id,_ := result.LastInsertId() fmt.Println("insert id is :",id) _, err = tx.Exec("update userinfo set department = ? where username = ?","cekong","hahah") if err != nil{ fmt.Println("update failed error:",err) tx.Rollback() return err } else { fmt.Println("update success!") } _, err = tx.Exec("delete from userinfo where username = ? ", "weiwei") if err != nil{ fmt.Println("delete error:",err) tx.Rollback() return err }else{ fmt.Println("delete success") } return tx.Commit() }
当然也可以用defer tx.Rollback(),在程序退出前回滚。
func mysqlTest() error{ tx, err := Db.Begin() if err != nil { log4go.Error("open mysql database fail", err) } defer tx.Rollback() result, err := tx.Exec("INSERT INTO userinfo (username, password,department,email) VALUES (?, ?,?,?)","cici","33333","it","TigerwolfC@163.com") if err != nil{ fmt.Println("insert failed,error: ", err) return err } id,_ := result.LastInsertId() fmt.Println("insert id is :",id) _, err = tx.Exec("update userinfo set department = ? where username = ?","cekong","hahah") if err != nil{ fmt.Println("update failed error:",err) return err } else { fmt.Println("update success!") } _, err = tx.Exec("delete from userinfo where username = ? ", "weiwei") if err != nil{ fmt.Println("delete error:",err) return err }else{ fmt.Println("delete success") } return tx.Commit() }
补充:数据库事务处理(go,mysql)
看代码吧~
//数据库连接池 var db *sql.DB //初始化数据库 func initDB() (err error){ fmt.Println("学习数据库") dsn := "usename:password@tcp(127.0.0.1:3306)/dataname" db, err = sql.Open("mysql",dsn) if err!=nil { fmt.Println("打开数据库失败 err:",err,".dsn:",dsn) return } err = db.Ping() if err!=nil { fmt.Println("open failed err:",err) return } fmt.Println("连接数据库成功") //最大连接数 db.SetMaxOpenConns(10) //设置连接池中的最大闲置连接数 db.SetMaxIdleConns(10) return } func trans() { tx, err := db.Begin() if err!=nil { fmt.Println("事务开启失败, err:",err) return } sqlStr1 := "update student set age=age-2 where id=1;" sqlStr2 := "update student set age=age+2 where id=3;" _, err = tx.Exec(sqlStr1) if err!=nil { fmt.Println("修改失败, err",err,",sqlStr1:",sqlStr1) tx.Rollback() return } _, err = tx.Exec(sqlStr2) if err!=nil { fmt.Println("修改失败, err",err,",sqlStr2:",sqlStr2) tx.Rollback() return } fmt.Println("执行成功") //提交事务 tx.Commit() } func main() { fmt.Println("mysql 002.事务处理") initDB() trans() }
上述就是小编为大家分享的怎么在golang中实现mysql数据库事务的提交与回滚了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。